iOS8开发之iOS8的UIAlertController

在iOS8之前用UIActionSheet和UIAlertView来提供按钮选择和提示性信息,比如UIActionSheet可以这样写:

[objc]  view plain copy
  1. UIActionSheet *actionSheet = [[UIActionSheet alloc]    
  2.                                  initWithTitle:@"title,nil时不显示"    
  3.                                  delegate:self    
  4.                                  cancelButtonTitle:@"取消"    
  5.                                  destructiveButtonTitle:@"确定"    
  6.                                  otherButtonTitles:@"第一项"@"第二项",nil];    
  7.    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;    
  8.    [actionSheet showInView:self.view];  

然后在协议中实现代理:

[objc]  view plain copy
  1. (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex    
  2. {    
  3.     if (buttonIndex == 0) {    
  4.         NSLog(@"确定");    
  5.     }else if (buttonIndex == 1) {    
  6.         NSLog(@"第一项");    
  7.     }else if(buttonIndex == 2) {    
  8.         NSLog(@"第二项");    
  9.     }else if(buttonIndex == actionSheet.cancleButtonIndex) {    
  10.         NSLog(@"取消");    
  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. }    

如果需要修改按钮字体、颜色等可以实现以下代理:

[objc]  view plain copy
  1. - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {  
  2.     for (UIView *subViwe in actionSheet.subviews) {  
  3.         if ([subViwe isKindOfClass:[UILabel class]]) {  
  4.             UILabel *label = (UILabel *)subViwe;  
  5.             label.font = [UIFont systemFontOfSize:16];  
  6.             label.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMinY(label.frame), CGRectGetWidth(label.frame), CGRectGetHeight(label.frame)+20);  
  7.         }  
  8.         if ([subViwe isKindOfClass:[UIButton class]]) {  
  9.             UIButton *button = (UIButton*)subViwe;  
  10.             if ([button.titleLabel.text isEqualToString:@"确定"]) {  
  11.                 [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];  
  12.             } else {  
  13.                 [button setTitleColor:[WTDevice getGreenColor] forState:UIControlStateNormal];  
  14.             }  
  15.             button.titleLabel.font = [UIFont systemFontOfSize:18];  
  16.         }  
  17.     }  
  18. }  

以上代码(代理部分),在ios7及以下版本中是有效的,但是在iOS8中却不起作用,因为iOS8抛弃了UIActionSheet和UIAlertView,取而代之的是UIAlertController,其使用方法如下(代替UIAlertView):

[objc]  view plain copy
  1. #ifdef __IPHONE_8_0  
  2.         if (TARGET_IS_IOS8) {  
  3.             UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"提示"  
  4.                                                                                            message:@"需要设置允许访问相机,操作方法见“设置”->“帮助中心”"  
  5.                                                                                     preferredStyle:UIAlertControllerStyleAlert];  
  6.             UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"确定"  
  7.                                                                    style:UIAlertActionStyleDestructive  
  8.                                                                  handler:^(UIAlertAction * action) {}];  
  9.               
  10.             [actionSheetController addAction:actionCancel];  
  11.             [actionSheetController.view setTintColor:[WTDevice getGreenColor]];  
  12.             [self presentViewController:actionSheetController animated:YES completion:nil];  
  13.         }  
  14. #endif  
  15.         if (TARGET_NOT_IOS8) {  
  16.             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"需要设置允许访问相机,操作方法见“设置”->“帮助中心”" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];  
  17.             [alert show];  
  18.         }  

代替UIActionSheet:

[objc]  view plain copy
  1. #ifdef __IPHONE_8_0  
  2.     if (TARGET_IS_IOS8) {  
  3.         UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"action选项"  
  4.                                                                                        message:nil  
  5.                                                                                 preferredStyle:UIAlertControllerStyleActionSheet];  
  6.         UIAlertAction *action0 = [UIAlertAction actionWithTitle:@"选项一"  
  7.                                                          style:UIAlertActionStyleDefault  
  8.                                                        handler:^(UIAlertAction * action) {  
  9.                                                            [self customMethod1];  
  10.                                                        }];  
  11.         [actionSheetController addAction:action0];  
  12.           
  13.         UIAlertAction *action = [UIAlertAction actionWithTitle:@"选项二"  
  14.                                                          style:UIAlertActionStyleDefault  
  15.                                                        handler:^(UIAlertAction * action) {  
  16.                                                            [self <span style="font-family: Arial, Helvetica, sans-serif;">customMethod2</span>];  
  17.                                                        }];  
  18.         UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"选项三"  
  19.                                                           style:UIAlertActionStyleDefault  
  20.                                                         handler:^(UIAlertAction * action) {  
  21.                                                             [self customMethod3];  
  22.                                                         }];  
  23.         UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消"  
  24.                                                                style:UIAlertActionStyleCancel  
  25.                                                              handler:^(UIAlertAction * action) {}];  
  26.           
  27.         [actionSheetController addAction:action];  
  28.         [actionSheetController addAction:action1];  
  29.         [actionSheetController addAction:actionCancel];  
  30.         [actionSheetController.view setTintColor:[UIColor greenColor]];  
  31.         [self presentViewController:actionSheetController animated:YES completion:nil];  
  32.     }  
  33. #endif  
  34.     if (TARGET_NOT_IOS8) {  
  35.         UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"action选项" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"选项一",@"选项二",@"选项三", nil nil];  
  36.         [as showInView:self.view];  
  37.     }  

至于两者的区别,可以看到,iOS8之前是在controller的view上边又覆盖了一层view,iOS8之后则是present了一个controller并且将代理换成了block,代码显得更加紧凑。

你可能感兴趣的:(iOS8开发之iOS8的UIAlertController)