UIActionSheet 是IOS中从底部弹出的按钮选项,可以为每一个选择设置触发事件。
为了快速完成这个例子我们在xcode中建立一个singleview 项目,在窗口中添加一个button用来弹出actionsheet
1.首先在头文件中声明协议<UIActionSheetDelegate>。
|
1
2
3
4
5
6 |
#import <UIKit/UIKit.h>@interface
sheetviewViewController : UIViewController<UIActionSheetDelegate>- (IBAction)showSheet:(id)sender;@end |
2.添加button。
3.为button建立action连线。
4.在m文件上添加事件代码,弹出actionsheet
|
1
2
3
4
5
6
7
8
9
10 |
- (IBAction)showSheet:(id)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"title,nil时不显示" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"第一项", @"第二项",nil]; actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; [actionSheet showInView:self.view];} |
5.设置actionsheet的样式。
actionsheet.actionSheetStyle=UIActionSheetStyleBlackOpaque /UIActionSheetStyleAutomatic /UIActionSheetStyleDefault /UIActionSheetStyleBlackTranslucent /UIActionSheetStyleBlackOpaque
6.显示actionsheet
调用showInView方法
7.响应actionsheet方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ if
(buttonIndex == 0) { [self
showAlert:@"确定"]; }else
if
(buttonIndex == 1) { [self
showAlert:@"第一项"]; }else
if(buttonIndex == 2) { [self
showAlert:@"第二项"]; }else
if(buttonIndex == 3) { [self
showAlert:@"取消"]; } } |
8.注意事项
在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是试用了UITabBar的时候才有。解决办法:
在 showView时这样使用,[actionSheet showInView:[UIApplication sharedApplication].keyWindow];或者[sheet showInView:[AppDelegate sharedDelegate].tabBarController.view];这样就不会发生遮挡现象了。
原文:http://www.cnblogs.com/jackwuyongxing/p/3514780.html