UIAlertController的使用
2015-12-28 10:45
651 查看
一 概述
在IOS8之后,UIAlertController替代了UIActionSheet和UIAlertView。把两种类型的提示信息放到这一个类里来实现。注意, 这个class不能通过继承的方式来自定义。
二 类介绍
先举两个使用的例子例子一
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet]; //添加Button [alertController addAction: [UIAlertAction actionWithTitle: @"拍照" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //处理点击拍照 }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"从相册选取" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action){ //处理点击从相册选取 }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"取消" style: UIAlertActionStyleCancel handler:nil]]; [self presentViewController: alertController animated: YES completion: nil];
例子二
实现代码
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"登陆" message: @"输入用户名密码" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"name"; textField.textColor = [UIColor blueColor]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.borderStyle = UITextBorderStyleRoundedRect; }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"password"; textField.textColor = [UIColor blueColor]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.borderStyle = UITextBorderStyleRoundedRect; textField.secureTextEntry = YES; }]; [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSArray * textfields = alertController.textFields; UITextField * namefield = textfields[0]; UITextField * passwordfiled = textfields[1]; NSLog(@"%@:%@",namefield.text,passwordfiled.text); }]]; [self presentViewController:alertController animated:YES completion:nil];
三 使用的步骤
第一步 初始化+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle
这里的preferredStyle有两种,sheet和alert
typedef enum UIAlertControllerStyle: NSInteger { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } UIAlertControllerStyle;
第二步,添加Action(button或者textfield)
添加Button
- (void)addAction:(UIAlertAction *)action
这里的UIAlertAction是一个比较简单的类
+ (instancetype)actionWithTitle:(NSString *)title style:(UIAlertActionStyle)style handler:(void (^)(UIAlertAction *action))handler
style有三种
typedef enum UIAlertActionStyle: NSInteger { UIAlertActionStyleDefault = 0,//默认 UIAlertActionStyleCancel,//取消 UIAlertActionStyleDestructive //有可能改变或者数据 } UIAlertActionStyle;
添加TextField
注意,只能是 UIAlertControllerStyleAlert才能添加Textfield
- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler
在block里配置textfield的信息,例如placeholder,backgroundcolor等。
Textfield的保存信息可由UIAlertController的属性Textfields获得。如同上述的例子二一样。
第三步,显示
例如
[self presentViewController:alert animated:YES completion:nil];
总结:
总的来说,API把两种Alertview进行统一,并且不用代理的方式来传递消息。而是改成了block。就个人而言,还是比较喜欢Block的方式的
相关文章推荐
- Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈
- iOS UIView视图抖动效果
- 12.27 jquerey 学习
- UIUILabel属性汇总
- android ui测试uiautomator
- 出现The requested resource is not available.错误
- Errors running builder 'DeploymentBuilder' on project '工程名' 错误解决方案
- EasyUI Form提交后json数据IE上需要下载(转)
- 【微信公众平台开发】公布动态新闻好帮手UEditor富文本
- 数据库序列( SEQUENCE)的使用
- UITableView的常用方法
- UIScrollView常用属性以及代理
- 手势返回控制器
- LeetCode:Unique Binary Search Trees
- php学习日记第十二棒(require、require_once、include、include_once)
- BlockingQueue
- UICollectionView基础
- 把arduino当AVRISP烧写器Arduino给Arduino烧boot
- UISearchDisplayController搜索栏控制器
- [LeetCode] 128. Longest Consecutive Sequence 解题思路