UIActionSheet的使用

来源:互联网 发布:apm2.8源码编译 编辑:程序博客网 时间:2024/06/01 10:19

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 公司用淘宝没发票做账怎么办 淘宝网买了假货确认了怎么办? 吃了安眠药睡了一天还没有醒怎么办 淘宝买的东西退货快递弄丢了怎么办 在淘宝上已付钱店家说没货了怎么办 从淘宝物流寄东西到国外被扣怎么办 不是天猫的淘宝卖家不发货怎么办 微店违规说卖假冒商品怎么办 云集微店的商品没货了怎么办 淘宝买家被检测有虚拟交易怎么办 媒体声音突然没有声音了该怎么办 华为微信运动步数为零怎么办 淘宝店铺没货了客户拍了怎么办 房子涨价了卖家反悔不卖了怎么办 买的东西很贵质量不好怎么办 在淘宝开的店账号忘了怎么办 建了个淘宝优惠券群没人购物怎么办 刚开的淘宝店没有生意怎么办 房产代理公司不给渠道结佣金怎么办 天猫超过72小时不发货怎么办 流量魔盒苹果下载怎么打不开怎么办 淘宝包邮店铺新疆地区拍怎么办 淘宝客服当顾客要优惠时怎么办 微信手机号注册的找不到了怎么办 之前注册的微信找不到了怎么办 苹果ipad的id密码忘了怎么办 淘宝和支付宝用一张银行卡怎么办 淘宝卖家填写虚假物流信息怎么办 淘宝店铺的浏览量越来越少怎么办 网上充手机话费充错了怎么办 夜神模拟器上陌陌的位置不对怎么办 如果在大庭广众之下放了个屁怎么办 淘宝分销上传宝贝被系统下架怎么办 酷狗喜欢歌单里面的歌都没了怎么办 苹果手机下载不了微信缓冲怎么办 登陆微信提示版本过低登不了怎么办 苹果手机微信版本过低登不上怎么办 微信小程序显示微信版本过低怎么办 三星手机登微信显示版本过低怎么办 微信版本低无法登录无法升级怎么办 手机淘宝五应用界面无法打开怎么办