UIActionSheet的使用详解

UIActionSheet是在IOS弹出的选择按钮项,可以添加多项,并为每项添加点击事件。

为了快速完成这例子,我们打开Xcode 4.3.2, 先建立一个single view application。然后再xib文件添加一个button,用来弹出sheet view。

1、首先在.h文件中实现协议,加代码的地方在@interface那行的最后添加<UIActionSheetDelegate>,协议相当于java里的接口,实现协议里的方法。

1@interface sheetviewViewController : UIViewController<UIActionSheetDelegate>
3@end
2、添加button,命名button为showSheetView.

3、为button建立Action映射,映射到.h文件上,事件类型为Action ,命名为showSheet。

4、在.m文件上添加点击事件代码

图的效果是这样的:

01- (IBAction)showSheet:(id)sender {
02    UIActionSheet *actionSheet = [[UIActionSheet alloc]
03                                  initWithTitle:@"title,nil时不显示"
04                                  delegate:self
05                                  cancelButtonTitle:@"取消"
06                                  destructiveButtonTitle:@"确定"
07                                  otherButtonTitles:@"第一项", @"第二项",nil];
08    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
09    [actionSheet showInView:self.view];
10}

actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式

参数解释:   

cancelButtonTitle  destructiveButtonTitle是系统自动的两项。

otherButtonTitles是自己定义的项,注意,最后一个参数要是nil。

[actionSheet showInView:self.view];这行语句的意思是在当前view显示Action sheet。当然还可以用其他方法显示Action sheet。

对应上面的图和代码,一目了然了把

5、接下来我们怎么相应Action Sheet的选项的事件呢?实现协议里的方法。为了能看出点击Action sheet每一项的效果,我们加入UIAlertView来做信息显示。下面是封装的一个方法,传入对应的信息,在UIAlertView显示对应的信息。

1-(void)showAlert:(NSString *)msg {
2    UIAlertView *alert = [[UIAlertView alloc]
3                          initWithTitle:@"Action Sheet选择项"
4                          message:msg
5                          delegate:self
6                          cancelButtonTitle:@"确定"
7                          otherButtonTitles: nil];
8    [alert show];
9}
那相应被Action Sheet选项执行的代码如下:
01(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
02{
03    if (buttonIndex == 0) {
04        [self showAlert:@"确定"];
05    }else if (buttonIndex == 1) {
06        [self showAlert:@"第一项"];
07    }else if(buttonIndex == 2) {
08        [self showAlert:@"第二项"];
09    }else if(buttonIndex == 3) {
10        [self showAlert:@"取消"];
11    }
12 
13}
14- (void)actionSheetCancel:(UIActionSheet *)actionSheet{ 
15 
16} 
17-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
18 
19} 
20-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{ 
21 
22}
可以看到 buttonIndex 是对应的项的索引。

看到那个红色的按钮没?那是ActionSheet支持的一种所谓的销毁按钮,对某户的某个动作起到警示作用,

比如永久性删除一条消息或图像时。如果你指定了一个销毁按钮他就会以红色高亮显示:

actionSheet.destructiveButtonIndex=1;  

与导航栏类似,操作表单也支持三种风格 

UIActionSheetStyleDefault              //默认风格:灰色背景上显示白色文字   

UIActionSheetStyleBlackTranslucent     //透明黑色背景,白色文字   

UIActionSheetStyleBlackOpaque          //纯黑背景,白色文字  

用法:

 actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;//设置样式

我选sheet 里的第一项,显示如下:

6、注意事项,在开发过程中,发现有时候UIActionSheet的最后一项点击失效,点最后一项的上半区域时有效,这是在特定情况下才会发生,这个场景就是试用了UITabBar的时候才有。解决办法:

在showView时这样使用,[actionSheet showInView:[UIApplication sharedApplication].keyWindow];或者[sheet showInView:[AppDelegate sharedDelegate].tabBarController.view];这样就不会发生遮挡现象了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值