UIActionSheet的使用

来源:互联网 发布:福连成老北京布鞋淘宝 编辑:程序博客网 时间:2024/05/18 14:42

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


[objc] view plain copy
  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
  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
  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
  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];  

原创粉丝点击