在iOS 8中使用UIAlertController

1. 最简单的提醒视图:

       这里我们实现一个最简单的提醒视图,包含1个标题,1行信息,1个按键,按下按键后,什么都不发生:

 

[objc]  view plain copy
  1. (IBAction)doAlert:(id)sender  
  2.     // 准备初始化配置参数  
  3.     NSString *title @"Alert Button Selected" 
  4.     NSString *message @"I need your attention NOW!" 
  5.     NSString *okButtonTitle @"OK" 
  6.       
  7.     // 初始化  
  8.     UIAlertController *alertDialog [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];  
  9.       
  10.     // 创建操作  
  11.     UIAlertAction *okAction [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)  
  12.         // 操作具体内容  
  13.         // Nothing to do.  
  14.     }];  
  15.       
  16.     // 添加操作  
  17.     [alertDialog addAction:okAction];  
  18.       
  19.     // 呈现警告视图  
  20.     [self presentViewController:alertDialog animated:YES completion:nil];  
  21.  
       进入程序后,点击“Alert Me!”按钮可触发这个提醒框,如图所示:

 

       从代码可以看出,新的API更加符合逻辑,有种需要什么就加什么的感觉。
2. 多个按键的提醒视图
       这里我们实现一个最简单的提醒视图,包含1个标题,1行信息,3个按键,按下按键后,标签显示按下的按键名称:
[objc]  view plain copy
  1. (IBAction)doMultiButtonAlert:(id)sender  
  2.     // 准备初始化配置参数  
  3.     NSString *title @"Alert Button Selected" 
  4.     NSString *message @"I need your attention NOW!" 
  5.     NSString *okButtonTitle @"OK" 
  6.     NSString *neverButtonTitle @"Never" 
  7.     NSString *laterButtonTitle @"Maybe Later" 
  8.       
  9.     // 初始化  
  10.     UIAlertController *alertDialog [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];  
  11.       
  12.     // 分别3个创建操作  
  13.     UIAlertAction *laterAction [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)  
  14.         // 普通按键  
  15.         self.userOutput.text @"Clicked 'Maybe Later'" 
  16.     }];  
  17.     UIAlertAction *neverAction [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)  
  18.         // 红色按键  
  19.         self.userOutput.text @"Clicked 'Never'" 
  20.     }];  
  21.     UIAlertAction *okAction [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)  
  22.         // 取消按键  
  23.         self.userOutput.text @"Clicked 'OK'" 
  24.     }];  
  25.       
  26.     // 添加操作(顺序就是呈现的上下顺序)  
  27.     [alertDialog addAction:laterAction];  
  28.     [alertDialog addAction:neverAction];  
  29.     [alertDialog addAction:okAction];  
  30.       
  31.     // 呈现警告视图  
  32.     [self presentViewController:alertDialog animated:YES completion:nil];  
  33.  
       3个按键分别代表了3种不同类型的按键,分别是默认按键(普通)、销毁按键(红色)和取消按键(粗体)。从代码看其实就是在上一个的基础上加了3个 UIAlertAction 而已,然后分别设置不同的 style,效果如下:

3. 带输入框的提醒视图
       如何添加输入框呢?新的 iOS 8 提供了相应的接口,使增加输入框就像增加按键方法一样简单。这里还是在第1个方法的基础上改动。
[objc]  view plain copy
  1. (IBAction)doAlertInput:(id)sender  
  2.     // 准备初始化配置参数  
  3.     NSString *title @"Email Address" 
  4.     NSString *message @"Please enter your your email address:" 
  5.     NSString *okButtonTitle @"OK" 
  6.       
  7.     // 初始化  
  8.     UIAlertController *alertDialog [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];  
  9.       
  10.     // 创建文本框  
  11.     [alertDialog addTextFieldWithConfigurationHandler:^(UITextField *textField){  
  12.         textField.placeholder @"Your Email" 
  13.         textField.secureTextEntry NO 
  14.     }];  
  15.       
  16.     // 创建操作  
  17.     UIAlertAction *okAction [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)  
  18.         // 读取文本框的值显示出来  
  19.         UITextField *userEmail alertDialog.textFields.firstObject 
  20.         self.userOutput.text userEmail.text 
  21.     }];  
  22.       
  23.     // 添加操作(顺序就是呈现的上下顺序)  
  24.     [alertDialog addAction:okAction];  
  25.       
  26.     // 呈现警告视图  
  27.     [self presentViewController:alertDialog animated:YES completion:nil];  
  28.  
       在创建操作前先创建文本框,以便后面的按键可以操作文本框内容。创建文本框也只是用了一个简单的方法而已,想创建更多文本框就再使用多次这个方法即可,程序效果如下:

4. 提醒图表
       与第2个和第3个方法相比,创建提醒图表简直易如反掌。因为和第1个方法相比,只需要改动一个参数就可以,即把创建UIAlertController实例的参数 UIAlertControllerStyleAlert 改为 UIAlertControllerStyleActionSheet ,别的都不用变。
[objc]  view plain copy
  1. (IBAction)doActionSheet:(id)sender  
  2.     // 准备初始化配置参数  
  3.     NSString *title @"Alert Button Selected" 
  4.     NSString *message @"I need your attention NOW!" 
  5.     NSString *okButtonTitle @"OK" 
  6.     NSString *neverButtonTitle @"Never" 
  7.     NSString *laterButtonTitle @"Maybe Later" 
  8.       
  9.     // 初始化  
  10.     UIAlertController *alertDialog [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];  
  11.       
  12.     // 分别3个创建操作  
  13.     UIAlertAction *laterAction [UIAlertAction actionWithTitle:laterButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)  
  14.         // 普通按键  
  15.         self.userOutput.text @"Clicked 'Maybe Later'" 
  16.     }];  
  17.     UIAlertAction *neverAction [UIAlertAction actionWithTitle:neverButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action)  
  18.         // 红色按键  
  19.         self.userOutput.text @"Clicked 'Never'" 
  20.     }];  
  21.     UIAlertAction *okAction [UIAlertAction actionWithTitle:okButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action)  
  22.         // 取消按键  
  23.         self.userOutput.text @"Clicked 'OK'" 
  24.     }];  
  25.       
  26.     // 添加操作(顺序就是呈现的上下顺序)  
  27.     [alertDialog addAction:laterAction];  
  28.     [alertDialog addAction:neverAction];  
  29.     [alertDialog addAction:okAction];  
  30.       
  31.     // 呈现警告视图  
  32.     [self presentViewController:alertDialog animated:YES completion:nil];  
  33.  
       这个就很简单了,跟第2个方法很像,效果如图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值