iOS8开发之iOS8的UIAlertController

简介: <p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 在iOS8之前用UIActionSheet和UIAlertView来提供按钮选择和提示性信息,比如UIActionSh

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

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  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 在CODE上查看代码片 派生到我的代码片
  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 在CODE上查看代码片 派生到我的代码片
  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 在CODE上查看代码片 派生到我的代码片
  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 在CODE上查看代码片 派生到我的代码片
  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,代码显得更加紧凑。
目录
相关文章
|
27天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
83 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
38 0
|
2天前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
【4月更文挑战第18天】**Cocoa Touch** 是iOS和Mac OS X应用的核心框架,包含面向对象库、运行时系统和触摸优化工具。它提供Mac验证的开发模式,强调触控接口和性能,涵盖3D图形、音频、网络及设备访问API,如相机和GPS。是构建高效iOS应用的基础,对开发者至关重要。
8 0
|
17天前
|
开发工具 Swift iOS开发
利用SwiftUI构建动态用户界面:iOS开发新范式
【4月更文挑战第3天】 随着苹果不断推进其软件开发工具的边界,SwiftUI作为一种新兴的编程框架,已经逐渐成为iOS开发者的新宠。不同于传统的UIKit,SwiftUI通过声明式语法和强大的功能组合,为创建动态且响应式的用户界面提供了一种更加简洁高效的方式。本文将深入探讨如何利用SwiftUI技术构建具有高度自定义能力和响应性的用户界面,并展示其在现代iOS应用开发中的优势和潜力。
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
181 2
|
3月前
|
数据可视化 iOS开发
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
iOS 开发,什么是 Interface Builder(IB)?如何使用 IB 构建用户界面?
40 4
|
3月前
|
iOS开发
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
26 1
|
3月前
|
存储 安全 数据安全/隐私保护
IOS开发数据存储:解释一下 iOS 中的 Keychain,它的作用是什么?
IOS开发数据存储:解释一下 iOS 中的 Keychain,它的作用是什么?
84 4
|
3月前
|
存储 数据库 iOS开发
IOS开发数据存储:什么是 CoreData?如何在应用中使用它?
IOS开发数据存储:什么是 CoreData?如何在应用中使用它?
34 0