UIAlertViewController的使用
UIAlertViewController是苹果自带的信息提示框,仅在iOS8.0以后可以使用
1 | NS_CLASS_AVAILABLE_IOS (8_0) @interface UIAlertController : UIViewController |
下面是一个使用的简单例子:
1 2 3 4 5 6 7 8 | UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@ "提示" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@ "取消" style:UIAlertActionStyleCancel handler: nil ]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@ "好的" style:UIAlertActionStyleDestructive handler: nil ]; [alertController addAction:cancelAction]; [alertController addAction:okAction]; [ self presentViewController:alertController animated: YES completion: nil ]; |
UIAlertController的alertControllerWithTitle:message:preferredStyle:方法中有个样式参数:preferredStyle
此参数为枚举类型:
1 2 3 4 | typedef NS_ENUM ( NSInteger , UIAlertControllerStyle) { UIAlertControllerStyleActionSheet = 0, UIAlertControllerStyleAlert } NS_ENUM_AVAILABLE_IOS (8_0); |
UIAlertControllerStyleAlert:该样式与UIAlertView的样式一样,显示在屏幕中央。
UIAlertControllerStyleActionSheet:该样式显示在屏幕底部,自下而上推出。
UIAlertAction的actionWithTitle:style:handler:方法中也有一个样式参数叫做:style
此参数为枚举类型:
1 2 3 4 5 | typedef NS_ENUM ( NSInteger , UIAlertActionStyle) { UIAlertActionStyleDefault = 0, UIAlertActionStyleCancel, UIAlertActionStyleDestructive } NS_ENUM_AVAILABLE_IOS (8_0); |
1 |
UIAlertActionStyleDefault是默认样式,白底黑字,响应touchUpInside事件。
UIAlertActionStyleCancel是取消样式,与UIAlertActionStyleDefault唯一的不同就是它的字体加粗了。
UIAlertActionStyleDestructive是具有警示性的样式,与UIAlertActionStyleDefault唯一的不同就是它的字体变为红色了。
有个问题:UIAlertController只能显示提示信息吗?如果是的话,为什么不继续使用UIAlertView?如果不是,那它还可以做什么?
答:不是。UIAlertController还可以添加文本输入框,与用户进行文本信息输入交互,而不再仅仅是输出给用户提示信息。接下来看一下一个简单的例子:
首先,将alertController声明为全局对象便于在整个类中使用
1 2 3 4 5 | @interface ViewController () @property ( nonatomic , strong) UIAlertController *alertController; @end |
接下来是实例化alertController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | self .alertController = [UIAlertController alertControllerWithTitle:@ "提示" message:str preferredStyle:UIAlertControllerStyleAlert]; [ self .alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.placeholder = @ "登录" ; [[ NSNotificationCenter defaultCenter] addObserver: self selector: @selector (alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; }]; [ self .alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.placeholder = @ "密码" ; textField.secureTextEntry = YES ; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@ "取消" style:UIAlertActionStyleCancel handler: nil ]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@ "确定" style:UIAlertActionStyleDestructive handler: nil ]; [okAction setEnabled: NO ]; [ self .alertController addAction:cancelAction]; [ self .alertController addAction:okAction]; [ self presentViewController: self .alertController animated: YES completion: nil ]; |
较之前面的代码,这里多了两行代码:
1 2 3 4 5 6 7 8 | [ self .alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.placeholder = @ "登录" ; [[ NSNotificationCenter defaultCenter] addObserver: self selector: @selector (alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; }]; [ self .alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){ textField.placeholder = @ "密码" ; textField.secureTextEntry = YES ; }]; |
这两行代码就是在alertController中加入文本输入框,让用户输入必要信息,以便交互。其中给textField设置了placeholder和secureTextEntry,另外还添加了属性监听。
1 | [[ NSNotificationCenter defaultCenter] addObserver: self selector: @selector (alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField]; |
此处的监听事件会在文本框变化时触发,方法如下:
1 2 3 4 5 6 7 8 9 10 11 | -( void )alertViewTextFieldDidChange:( NSNotification *)notification { UITextField *textField = notification.object; UIAlertAction *action = self .alertController.actions[1]; [action setEnabled: NO ]; if (textField.text.length>3) { NSLog (@ "textField.text = %@" ,textField.text); [action setEnabled: YES ]; } } |
方法中通过:notification.object来获取到输入框,用于后续操作。
http://www.cnblogs.com/PaulpauL/
版权声明:本文为博主原创文章,未经博主允许不得转载。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了