iOS8- UIAlertViewController
2016-03-06 09:57
453 查看
http://blog.csdn.net/xiaowenwen1010/article/details/40108097
iOS8新推出的UIAlertController究竟是为了什么?已有的
PS:其实最直接的原因就是博主想弹出个Alert,有俩文本框的(不要密码输入框),可惜
创建很简单有木有:
举个栗子:
以前的做法是给个按钮标题数组然后在实现协议的代理方法中判断下按钮序列,然后对应给出不同的处理流程。分散的逻辑和代码无疑增加了开发者的工作量,还要求对应的类实现代理协议,真是out了。
这里介绍一个新的类:UIAlertAction,
这也是最令博主激动的地方,最然不能自定义Alert中的内容,但起码添加UITextField不受限制了。添加文本框的方法实在是简洁:
一个block就解决了!你只需要在block中配置下文本框的字体、键盘、代理等即可,举个栗子:
美中不足的是文本框的代理还需要在另外一个地方写代码来实现协议,这种历史遗留问题终将被block统统解决!
最后,贴上一张效果图:
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
创建很简单有木有:<span style="font-family:Microsoft YaHei;"><span class="line-number" style="margin: 0px; padding: 0px;">1</span> <span class="line-number" style="margin: 0px; padding: 0px;">2</span> <span class="line-number" style="margin: 0px; padding: 0px;">3</span> </span> | convenience init(title title: String?, message message: String?, preferredStyle preferredStyle: UIAlertControllerStyle) |
<span style="font-family:Microsoft YaHei;"><span class="line-number" style="margin: 0px; padding: 0px;">1</span> <span class="line-number" style="margin: 0px; padding: 0px;">2</span> <span class="line-number" style="margin: 0px; padding: 0px;">3</span> </span> | let title = "Enter Choices of the Trouble" let message = detailItem?.content let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert) |
title和
message自然不用说了吧,需要注意的是
preferredStyle一旦设定好以后就不能改了哦,因为它就是区分
UIAlertController到底是对应着
UIActionSheet还是
UIAlertView,一共就这两种Style:
<span style="font-family:Microsoft YaHei;"><span class="line-number" style="margin: 0px; padding: 0px;">1</span> <span class="line-number" style="margin: 0px; padding: 0px;">2</span> <span class="line-number" style="margin: 0px; padding: 0px;">3</span> <span class="line-number" style="margin: 0px; padding: 0px;">4</span> </span> | enum UIAlertControllerStyle : Int { case ActionSheet case Alert } |
添加动作
以前的做法是给个按钮标题数组然后在实现协议的代理方法中判断下按钮序列,然后对应给出不同的处理流程。分散的逻辑和代码无疑增加了开发者的工作量,还要求对应的类实现代理协议,真是out了。这里介绍一个新的类:UIAlertAction,
UIAlertController弱化了以前添加Button的概念,而是添加UIAlertAction。其实说白了就是将按钮和处理方法捏到一起成为一个“动作”:
<span style="font-family:Microsoft YaHei;"><span class="line-number" style="margin: 0px; padding: 0px;">1</span> <span class="line-number" style="margin: 0px; padding: 0px;">2</span> <span class="line-number" style="margin: 0px; padding: 0px;">3</span> </span> | convenience init(title title: String, style style: UIAlertActionStyle, handler handler: ((UIAlertAction!) -> Void)!) |
title就是按钮的文本内容;
style是按钮的风格,一共有三种风格:Default,Cancel和Destructive,其中Destructive会使按钮文字变成红色;
handler是处理按钮按下后的一个block,这段代码块与添加的按钮紧密结合成“动作”,最后通过
addAction:方法将UIAlertAction添加到
UIAlertController中:
<span style="font-family:Microsoft YaHei;"><span class="line-number" style="margin: 0px; padding: 0px;">1</span> <span class="line-number" style="margin: 0px; padding: 0px;">2</span> <span class="line-number" style="margin: 0px; padding: 0px;">3</span> <span class="line-number" style="margin: 0px; padding: 0px;">4</span> <span class="line-number" style="margin: 0px; padding: 0px;">5</span> </span> | let cancelbtn = "Cancel" let cancelAction = UIAlertAction(title: cancelbtn, style: .Cancel) { (action) -> Void in //add some code... } alert.addAction(cancelAction) |
添加文本框
这也是最令博主激动的地方,最然不能自定义Alert中的内容,但起码添加UITextField不受限制了。添加文本框的方法实在是简洁:<span style="font-family:Microsoft YaHei;"><span class="line-number" style="margin: 0px; padding: 0px;">1</span> </span> | func addTextFieldWithConfigurationHandler(_ configurationHandler: ((UITextField!) -> Void)!) |
<span style="font-family:Microsoft YaHei;"><span class="line-number" style="margin: 0px; padding: 0px;">1</span> <span class="line-number" style="margin: 0px; padding: 0px;">2</span> <span class="line-number" style="margin: 0px; padding: 0px;">3</span> <span class="line-number" style="margin: 0px; padding: 0px;">4</span> <span class="line-number" style="margin: 0px; padding: 0px;">5</span> <span class="line-number" style="margin: 0px; padding: 0px;">6</span> </span> | alert.addTextFieldWithConfigurationHandler { (choiceNameTF) -> Void in choiceNameTF.borderStyle = .None choiceNameTF.placeholder = "An answer of your trouble" choiceNameTF.delegate = self choiceNameTF.becomeFirstResponder() } |
总结
UIAlertController的确是方便多了,无论是代码的简洁性还是可读性,都有了较大的提升。不过也有缺点,就是动用Controller的层级略显麻烦,尤其是使用
presentViewController:animated:completion:方法来弹出Alert界面与之前的一个潇洒的
show相比真是有些“霸气”。看个人喜好了,只要满足需求,咋方便咋用。
最后,贴上一张效果图:
相关文章推荐
- IOS UIAlertView(警告框)方法总结
- 编译samba遇到need autoconf 2.53 or later to build samba from GIT
- UITableView
- UIScrollView和MultiThreading、UITextField、Keyboard
- 微软Build2016:Win10 RS1红石正式版有哪些新功能?什么时候正式发布?
- Ubuntu下android studio 编译报错A problem occurred starting process 'command '/Android/Sdk/build-tools/21.1.2/aapt''
- Win10 Build 14279详细测评 新增功能大全
- Win10 Build 14279正式推送 更新后QQ可能会崩溃
- 如何设置约束让三个按钮平分UIView容器的宽度
- 自定义一个只显示年月的DatePicker(UIDatePicker无法实现年月显示)
- LeetCode Range Sum Query 2D - Mutable
- build、compile、rebuild all
- IOS uitableview中cell的分割线设置距离,并且去掉cell的边框,要不然多了一条细线
- IOS之UI--动态设置NavigationBar的颜色以及透明度
- IOS 日期选择器 UIDatePicker的使用
- IOS UITableViewCell样式
- android studio 报Error:failed to find Build Tools revision 23.0.0 rc2
- 【Monotonic-queue】【dp】【Segment-tree】【STL】Codeforces 487B - Strip
- GCD中有哪几种Queue?你自己建立过串行Queue吗?背后的线程模型是什么样的
- 如何高性能的给UIImageView加个圆角