UIActionViewController 详解 iOS8

- - (void)showOkayCancelAlert {
- NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
- NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
- NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
- NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
- // Create the actions.
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
- }];
- UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
- }];
- // Add the actions.
- [alertController addAction:cancelAction];
- [alertController addAction:otherAction];
- [self presentViewController:alertController animated:YES completion:nil];
- }

这是最普通的一个alertcontroller,一个取消按钮,一个确定按钮。
新的alertcontroller,其初始化方法也不一样了,按钮响应方法绑定使用了block方式,有利有弊。需要注意的是不要因为block导致了引用循环,记得使用__weak,尤其是使用到self。
上面的界面如下:
如果UIAlertAction *otherAction这种otherAction多几个的话,它会自动排列成如下:
另外,很多时候,我们需要在alertcontroller中添加一个输入框,例如输入密码:
这时候可以添加如下代码:
- [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
- // 可以在这里对textfield进行定制,例如改变背景色
- textField.backgroundColor = [UIColor orangeColor];
- }];
而改变背景色会这样:
完整的密码输入:

- - (void)showSecureTextEntryAlert {
- NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
- NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
- NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
- NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
- // Add the text field for the secure text entry.
- [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
- // Listen for changes to the text field's text so that we can toggle the current
- // action's enabled property based on whether the user has entered a sufficiently
- // secure entry.
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
- textField.secureTextEntry = YES;
- }];
- // Create the actions.
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- NSLog(@"The \"Secure Text Entry\" alert's cancel action occured.");
- // Stop listening for text changed notifications.
- [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
- }];
- UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- NSLog(@"The \"Secure Text Entry\" alert's other action occured.");
- // Stop listening for text changed notifications.
- [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
- }];
- // The text field initially has no text in the text field, so we'll disable it.
- otherAction.enabled = NO;
- // Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
- self.secureTextAlertAction = otherAction;
- // Add the actions.
- [alertController addAction:cancelAction];
- [alertController addAction:otherAction];
- [self presentViewController:alertController animated:YES completion:nil];
- }

注意四点:
1.添加通知,监听textfield内容的改变:

- // Add the text field for the secure text entry.
- [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
- // Listen for changes to the text field's text so that we can toggle the current
- // action's enabled property based on whether the user has entered a sufficiently
- // secure entry.
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
- textField.secureTextEntry = YES;
- }];

2.初始化时候,禁用“ok”按钮:
otherAction.enabled = NO;
self.secureTextAlertAction = otherAction;//定义一个全局变量来存储
3.当输入超过5个字符时候,使self.secureTextAlertAction = YES:
- - (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {
- UITextField *textField = notification.object;
- // Enforce a minimum length of >= 5 characters for secure text alerts.
- self.secureTextAlertAction.enabled = textField.text.length >= 5;
- }
4.在“OK”action中去掉通知:

- UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- NSLog(@"The \"Secure Text Entry\" alert's other action occured.");
- // Stop listening for text changed notifications.
- [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
- }];

最后是以前经常是alertview与actionsheet结合使用,这里同样也有:

- - (void)showOkayCancelActionSheet {
- NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
- NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil);
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- // Create the actions.
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- NSLog(@"The \"Okay/Cancel\" alert action sheet's cancel action occured.");
- }];
- UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
- NSLog(@"The \"Okay/Cancel\" alert action sheet's destructive action occured.");
- }];
- // Add the actions.
- [alertController addAction:cancelAction];
- [alertController addAction:destructiveAction];
- [self presentViewController:alertController animated:YES completion:nil];
- }

在底部显示如下:
好了,至此,基本就知道这个新的controller到底是怎样使用了。
UIActionViewController 详解 iOS8的更多相关文章
- UITextField使用详解
转iOS中UITextField使用详解 (1) //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFr ...
- IOS中表视图(UITableView)使用详解
IOS中UITableView使用总结 一.初始化方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styl ...
- iOS开发——屏幕适配篇&Masonry详解
Masonry详解 前言 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-ip ...
- iOS 开发之照片框架详解
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...
- 《招一个靠谱的移动开发》iOS面试题及详解(下篇)
iOS面试知识点 现在进入本篇的正题.本篇的面试题是我认为比较好的iOS开发基础知识点,希望大家看过这后在理解的基础上掌握而不是死记硬背.死记硬背很快也会忘记的. 1 iOS基础 1.1 父类实现深拷 ...
- 了解iOS消息推送一文就够:史上最全iOS Push技术详解
本文作者:陈裕发, 腾讯系统测试工程师,由腾讯WeTest整理发表. 1.引言 开发iOS系统中的Push推送,通常有以下3种情况: 1)在线Push:比如QQ.微信等IM界面处于前台时,聊天消息和指 ...
- iOS百度地图简单使用详解
iOS百度地图简单使用详解 百度地图 iOS SDK是一套基于iOS 5.0及以上版本设备的应用程序接口,不仅提供展示地图的基本接口,还提供POI检索.路径规划.地图标注.离线地图.定位.周边雷达等丰 ...
- NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY
NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY 转载2015-07-08 18:02:02 鉴于苹果官方文档的解释 ...
- TableView 常用技巧与功能详解
分割线顶格iOS8 UITableview分割线顶格的做法 //iOS8 Cell分割线顶格 if ([_tableView respondsToSelector:@selector(setSepar ...
随机推荐
- linux ssh scp无密码登录
一. 应用场景 假如你Linux Client是客户端, Server为服务器,用户名为user.现在要配置从Client到Server的无密码SSH登录或者无密码的scp拷贝. 例如客户端Clien ...
- 英文Ubantu系统安装中文输入法
以前都是安装的中文Ubantu,但是有时候用命令行的时候中文识别不好,会出现错误,所以这次安装了英文版,但是安装后发现输入法不好用,于是就要自己安装输入法. 安装环境为Ubantu13.04 1.卸载 ...
- Cocos2d-js 3.0 alp2 使用指南
Download Cocos2d-JS: http://www.cocos2d-x.org/download Unzip and copy to C:/ Download JDK: http://ww ...
- W5500问题集锦(持续更新中)
在"WIZnet杯"以太网技术竞赛中,有非常多參赛者在使用中对W5500有各种各样的疑问,对于这款WIZnet新推出的以太网芯片,使用中大家是不是也一样存在下面问题呢?来看一看: ...
- docker-proxy 实现容器代理访问
可实现多个容器web主机对外提供访问 运行代理容器 nginx-proxy docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.soc ...
- unix进程的环境--unix环境高级编程读书笔记
http://blog.csdn.net/xiaocainiaoshangxiao/article/category/1800937
- cookie 和 HttpSession
保存会话数据的两种技术 Cookie Cookie 是客户端技术,程序把每个用户的数据以cookie的形式写给用户的浏览器.当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去.web ...
- 高效 jquery 的奥秘
当你准备使用 jQuery,我强烈建议你遵循下面这些指南: 1. 缓存变量 DOM 遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $( ...
- MSDN无法显示该页的解决办法
今天打开msdn,发现 查阅api时候 出现 “无法显示该页的解决办法“ 这个问题.解决方案如下: 在“运行”中输入regsvr32 "C:\Program Files\Common Fil ...
- Objective-C 内存管理与高级环境编程 阅读分享
常用的调试私有API uintptr_t objc_rootRetainCount(id obj) _objc_autoreleasePoolPrint();//查看自动释放池中的对象 LLVM cl ...