iOS8- UIAlertViewController
来源:互联网 发布:淘宝佣金怎么计算 编辑:程序博客网 时间:2024/06/05 03:55
iOS8新推出的UIAlertController究竟是为了什么?已有的UIActionSheet
和UIAlertView
就这样被打酱油了么?其实不然。。。
UIAlertController是什么
UIAlertController
是iOS8中新加入的一个ViewController,其主要功能是提醒(Alert),并取代UIActionSheet
和UIAlertView
向用户展示提醒消息。因为它是一个ViewController,所以可以用presentViewController:animated:completion:
方法来弹出它。
为什么不直接使用UIActionSheet
和UIAlertView
UIAlertController
采用block异步回调的形式来代理按钮的动作和文本框的配置,这样显得更加代码紧凑,可读性高。这样就不用通过协议代理给其他类,使得同一个逻辑下本应在一起的代码分离开。UIActionSheet
和UIAlertView
执行的都是提醒功能,只是展现的位置略有不同,合并相似功能并上升到Controller的级别,这是一种进步。UIAlertController
在添加按钮和文本框时更加灵活,而UIAlertView
只提供了四种Style,局限性太大。UIAlertController
在增加拓展性的基础上也限定了一些风格来进行约束,不过新增加的风格比UIAlertView
更贴合实际应用。
PS:其实最直接的原因就是博主想弹出个Alert,有俩文本框的(不要密码输入框),可惜UIAlertView
做不到啊!!!
创建一个UIAlertController
创建很简单有木有:
123
convenience init(title title: String?, message message: String?, preferredStyle preferredStyle: UIAlertControllerStyle)
举个栗子:
123
let title = "Enter Choices of the Trouble"let message = detailItem?.contentlet alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
title
和message
自然不用说了吧,需要注意的是preferredStyle
一旦设定好以后就不能改了哦,因为它就是区分UIAlertController
到底是对应着UIActionSheet
还是UIAlertView
,一共就这两种Style:
1234
enum UIAlertControllerStyle : Int { case ActionSheet case Alert}
添加动作
以前的做法是给个按钮标题数组然后在实现协议的代理方法中判断下按钮序列,然后对应给出不同的处理流程。分散的逻辑和代码无疑增加了开发者的工作量,还要求对应的类实现代理协议,真是out了。
这里介绍一个新的类:UIAlertAction,UIAlertController
弱化了以前添加Button的概念,而是添加UIAlertAction。其实说白了就是将按钮和处理方法捏到一起成为一个“动作”:
123
convenience init(title title: String, style style: UIAlertActionStyle, handler handler: ((UIAlertAction!) -> Void)!)
title
就是按钮的文本内容;style
是按钮的风格,一共有三种风格:Default,Cancel和Destructive,其中Destructive会使按钮文字变成红色;handler
是处理按钮按下后的一个block,这段代码块与添加的按钮紧密结合成“动作”,最后通过addAction:
方法将UIAlertAction添加到UIAlertController
中:
12345
let cancelbtn = "Cancel"let cancelAction = UIAlertAction(title: cancelbtn, style: .Cancel) { (action) -> Void in//add some code... }alert.addAction(cancelAction)
添加文本框
这也是最令博主激动的地方,最然不能自定义Alert中的内容,但起码添加UITextField不受限制了。添加文本框的方法实在是简洁:
1
func addTextFieldWithConfigurationHandler(_ configurationHandler: ((UITextField!) -> Void)!)
一个block就解决了!你只需要在block中配置下文本框的字体、键盘、代理等即可,举个栗子:
123456
alert.addTextFieldWithConfigurationHandler { (choiceNameTF) -> Void in choiceNameTF.borderStyle = .None choiceNameTF.placeholder = "An answer of your trouble" choiceNameTF.delegate = self choiceNameTF.becomeFirstResponder()}
美中不足的是文本框的代理还需要在另外一个地方写代码来实现协议,这种历史遗留问题终将被block统统解决!
总结
UIAlertController
的确是方便多了,无论是代码的简洁性还是可读性,都有了较大的提升。不过也有缺点,就是动用Controller的层级略显麻烦,尤其是使用presentViewController:animated:completion:
方法来弹出Alert界面与之前的一个潇洒的show
相比真是有些“霸气”。看个人喜好了,只要满足需求,咋方便咋用。
最后,贴上一张效果图:
图片截取自HardChoice
- iOS8- UIAlertViewController
- iOS8- UIAlertViewController
- iOS8之后的UIAlertViewController
- 在iOS8中使用UIAlertViewController
- iOS8新特性UIAlertViewController,UISearchController,UIPopoverPresentationController,WKWebView
- UIAlertViewController
- UIAlertViewController
- UIAlertViewController
- UIAlertViewController详解
- KVO 和 UIAlertViewController
- 使用UIAlertviewController弹出视图
- iOS UIALertViewController常用方法
- UIAlertViewController的使用方法
- iOS 9 的 UIAlertViewController
- Swift 提示框 UIAlertViewController
- 自定义UIAlertViewController选择视图
- ios8
- iOS8
- VS2008搭建驱动开发环境
- Flume学习(一)Flume初始
- 数据库使用--MySQL: InnoDB 还是 MyISAM?
- 友元类
- 中国医药产业黄金十年的投资地图
- iOS8- UIAlertViewController
- android 相机拍照后选择照片编辑,相片编辑界面直线形状会显示锯齿状
- java.lang.NullPointerException at com.android.volley.Request.<init>(Request.java:148)
- error和exception的区别,RuntimeException和非RuntimeException的区别
- Android编码规范
- VS的BUG积累
- MYSQL性能优化的二十一条经验
- 关于VC6.0编译时的问题fatal error LNK1104
- android 在图库中进行幻灯片播放时图片不会全屏显示