UIActionSheet的使用

UIActionSheet alertview相似,同样也是弹框提示,不同的地方在于actionsheet是靠底端显示,而alertview是居中显示。


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方法1 无代理,只有2个确定按钮  
  2. UIActionSheet *actionsheet01 = [[UIActionSheet alloc] initWithTitle:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"知道了", nil nil];  
  3. // 显示  
  4. [actionsheet01 showInView:self.view];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方法2 无代理,有多个确定按钮  
  2. UIActionSheet *actionsheet02 = [[UIActionSheet alloc] initWithTitle:@"按钮点击后我才出现的。" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"知道了0"@"知道了1"@"知道了2"@"知道了3", nil nil];  
  3. // 显示  
  4. [actionsheet02 showInView:self.view];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方法3 有代理,有2个确定按钮  
  2. /* 
  3. 1 设置代理为 self 
  4. 2 添加协议 
  5. 3 实现方法 
  6. */  
  7. UIActionSheet *actionsheet03 = [[UIActionSheet alloc] initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册"@"拍照", nil nil];  
  8. // 显示  
  9. [actionsheet03 showInView:self.view];  
  10.   
  11. // 添加协议  
  12. @interface ViewController () <UIActionSheetDelegate>  
  13.   
  14. @end  
  15.   
  16. // UIActionSheetDelegate实现代理方法  
  17. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  
  18. {  
  19.     NSLog(@"buttonIndex=%ld", buttonIndex);  
  20.       
  21.     // 方法1  
  22. //    if (0 == buttonIndex)  
  23. //    {  
  24. //        NSLog(@"点击了相册按钮");  
  25. //    }  
  26. //    else if (1 == buttonIndex)  
  27. //    {  
  28. //        NSLog(@"点击了拍照按钮");  
  29. //    }  
  30. //    else if (2 == buttonIndex)  
  31. //    {  
  32. //        NSLog(@"点击了取消按钮");  
  33. //    }  
  34.       
  35.     // 方法2  
  36.     NSString *title = [actionSheet buttonTitleAtIndex:buttonIndex];  
  37.     BOOL isTakePhoto = [title isEqualToString:@"拍照"];  
  38.     BOOL isPhotos = [title isEqualToString:@"相册"];  
  39.     if (isTakePhoto)  
  40.     {  
  41.         NSLog(@"点击了拍照按钮");  
  42.     }  
  43.     else if (isPhotos)  
  44.     {  
  45.         NSLog(@"点击了相册按钮");  
  46.     }  
  47.     else  
  48.     {  
  49.         NSLog(@"点击了取消按钮");  
  50.     }  
  51. }  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 方法4  
  2. /* 
  3. iOS8以后出现了UIAlertController视图控制器,通过设置UIAlertController的style属性来控制是alertview还是actionsheet 
  4. */  
  5. UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:@"选择图片" preferredStyle:UIAlertControllerStyleActionSheet];  
  6. // 响应方法-取消  
  7. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {  
  8.         NSLog(@"点击了取消按钮");  
  9. }];  
  10. // 响应方法-相册  
  11. UIAlertAction *takeAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {  
  12.         NSLog(@"点击了相册按钮");  
  13. }];  
  14. // 响应方法-拍照  
  15. UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {  
  16.         NSLog(@"点击了拍照按钮");  
  17. }];  
  18. // 添加响应方式  
  19. [actionSheetController addAction:cancelAction];  
  20. [actionSheetController addAction:takeAction];  
  21. [actionSheetController addAction:photoAction];  
  22. // 显示  
  23. [self presentViewController:actionSheetController animated:YES completion:nil];  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值