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,代码显得更加紧凑。
目录
相关文章
|
5天前
|
Unix 调度 Swift
苹果iOS新手开发之Swift 中获取时间戳有哪些方式?
在Swift中获取时间戳有四种常见方式:1) 使用`Date`对象获取秒级或毫秒级时间戳;2) 通过`CFAbsoluteTimeGetCurrent`获取Core Foundation的秒数,需转换为Unix时间戳;3) 使用`DispatchTime.now()`获取纳秒级精度的调度时间点;4) `ProcessInfo`提供设备启动后的秒数,不表示绝对时间。不同方法适用于不同的精度和场景需求。
17 3
|
4天前
|
Swift iOS开发 Kotlin
苹果iOS新手开发之Swift中实现类似Kotlin的作用域函数
Swift可通过扩展实现类似Kotlin作用域函数效果。如自定义`let`, `run`, `with`, `apply`, `also`,增强代码可读性和简洁性。虽无直接内置支持,但利用Swift特性可达成相似功能。
23 7
|
1天前
|
移动开发 前端开发 iOS开发
探索iOS开发的未来:SwiftUI与Combine的融合
随着苹果公司不断推进其操作系统的更新,iOS开发领域也迎来了诸多变革。在这篇文章中,我们将深入探讨SwiftUI和Combine这两个强大的框架,它们如何共同塑造着iOS应用开发的未来趋势。通过具体实例和数据支持,本文旨在揭示这些技术如何简化开发者的工作流,提升用户界面构建的效率,以及加强应用的响应性和性能表现。我们还将提出一个开放性问题,邀请读者思考并探索这些技术在未来可能带来的进一步影响。 【7月更文挑战第26天】
9 2
|
3天前
|
API 数据处理 开发工具
探索iOS开发的未来:SwiftUI和Combine的融合
【7月更文挑战第23天】随着Apple不断推动其软件开发工具的创新,SwiftUI和Combine框架的出现标志着iOS开发进入了一个新的时代。本文将深入探讨这两个框架如何简化界面设计和事件处理,以及它们如何共同为开发者提供一个更加高效、声明式的编程模型。我们将通过实际示例来展示如何利用SwiftUI构建用户界面,并使用Combine处理异步事件和状态管理。文章还将预测这些技术如何塑造iOS应用开发的未来趋势。
|
2天前
|
调度 Swift Android开发
苹果iOS新手开发之Swift中的并发任务和消息机制
Swift的消息机制类似Android的Handler,实现任务调度有三种方式: 1. **Grand Central Dispatch (GCD)**:使用`DispatchQueue`在主线程或后台线程执行任务。 2. **OperationQueue**:提供高级接口管理`Operation`对象。 3. **RunLoop**:处理事件如输入源、计时器,类似Android的`Looper`和`Handler`。 **示例**: - GCD:在不同线程执行代码块。 - OperationQueue:创建操作并执行。 - RunLoop:用Timer添加到RunLoop中。
12 2
|
7天前
|
Swift iOS开发 开发者
探索iOS开发中的SwiftUI框架
【7月更文挑战第19天】在移动应用开发的浪潮中,苹果公司的SwiftUI框架如同一股清新的海风,为iOS开发者带来了前所未有的编程体验。本文将深入探讨SwiftUI的核心特性,揭示其如何简化界面设计流程,提升开发效率,并展望SwiftUI在未来iOS开发领域的发展潜力。通过实例分析,我们将一同见证SwiftUI如何塑造更加直观、高效的编程模式。
|
3天前
|
前端开发 Android开发 iOS开发
探索安卓与iOS开发的差异性与互补性
在移动应用开发的广阔舞台上,安卓和iOS这两大操作系统各据一方,引领着市场潮流。它们在技术架构、开发环境及用户群体等方面展现出独特的差异性,同时也存在着潜在的互补性。本文将深入剖析这两种平台的开发细节,从不同角度揭示其各自优势及相互之间的协同潜力,为开发者提供全面而深刻的视角。
10 2
|
4天前
|
前端开发 Swift iOS开发
探索iOS开发的未来:SwiftUI和Combine的融合
在iOS开发领域,SwiftUI和Combine框架的出现标志着一个新时代的到来。本文深入探讨这两个框架如何共同推动iOS应用开发的现代化,通过具体案例分析它们的优势、挑战以及未来趋势。
|
4天前
|
数据处理 Swift iOS开发
探索iOS开发的未来之路:SwiftUI和Combine框架的融合
在本文中,我们将深入探讨iOS开发的新趋势——SwiftUI和Combine框架的结合使用。我们将从这两个框架的基本概念入手,逐步解析它们如何协同工作,以实现更加高效、响应式的用户界面构建。通过实例演示,我们将揭示这种组合如何简化代码结构,提高开发效率,并增强应用性能。最后,我们将展望这种技术栈在未来iOS开发中的潜在影响和应用前景。
|
7天前
|
开发工具 Android开发 Swift
探索Android与iOS开发的差异与挑战
【7月更文挑战第20天】在移动应用开发的广阔天地中,Android和iOS两大平台如同双子星座,各自闪耀着独特的光芒。本文将深入探讨这两个平台在开发过程中的主要差异,以及开发者面临的技术挑战。我们将从开发环境、编程语言、用户界面设计、性能优化、安全性考量等多个维度展开讨论,旨在为那些即将踏入或已在这片星空下航行的开发者提供一盏明灯。