iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了~但是也无所谓,有新东西不怕,学会使用了就行。接下来会探讨一下这些个新的Controller。
  1. - (void)showOkayCancelAlert {
  2. NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
  3. NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
  4. NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
  5. NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);
  6.  
  7. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  8.  
  9. // Create the actions.
  10. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  11. NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
  12. }];
  13.  
  14. UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  15. NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
  16. }];
  17.  
  18. // Add the actions.
  19. [alertController addAction:cancelAction];
  20. [alertController addAction:otherAction];
  21.  
  22. [self presentViewController:alertController animated:YES completion:nil];
  23. }

这是最普通的一个alertcontroller,一个取消按钮,一个确定按钮。

新的alertcontroller,其初始化方法也不一样了,按钮响应方法绑定使用了block方式,有利有弊。需要注意的是不要因为block导致了引用循环,记得使用__weak,尤其是使用到self。

上面的界面如下:

如果UIAlertAction *otherAction这种otherAction多几个的话,它会自动排列成如下:

另外,很多时候,我们需要在alertcontroller中添加一个输入框,例如输入密码:

这时候可以添加如下代码:

  1. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  2. // 可以在这里对textfield进行定制,例如改变背景色
  3. textField.backgroundColor = [UIColor orangeColor];
  4. }];

而改变背景色会这样:

完整的密码输入:

  1. - (void)showSecureTextEntryAlert {
  2. NSString *title = NSLocalizedString(@"A Short Title Is Best", nil);
  3. NSString *message = NSLocalizedString(@"A message should be a short, complete sentence.", nil);
  4. NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
  5. NSString *otherButtonTitle = NSLocalizedString(@"OK", nil);
  6.  
  7. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  8.  
  9. // Add the text field for the secure text entry.
  10. [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  11. // Listen for changes to the text field's text so that we can toggle the current
  12. // action's enabled property based on whether the user has entered a sufficiently
  13. // secure entry.
  14. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
  15.  
  16. textField.secureTextEntry = YES;
  17. }];
  18.  
  19. // Create the actions.
  20. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  21. NSLog(@"The \"Secure Text Entry\" alert's cancel action occured.");
  22.  
  23. // Stop listening for text changed notifications.
  24. [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
  25. }];
  26.  
  27. UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  28. NSLog(@"The \"Secure Text Entry\" alert's other action occured.");
  29.  
  30. // Stop listening for text changed notifications.
  31. [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.firstObject];
  32. }];
  33.  
  34. // The text field initially has no text in the text field, so we'll disable it.
  35. otherAction.enabled = NO;
  36.  
  37. // Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.
  38. self.secureTextAlertAction = otherAction;
  39.  
  40. // Add the actions.
  41. [alertController addAction:cancelAction];
  42. [alertController addAction:otherAction];
  43.  
  44. [self presentViewController:alertController animated:YES completion:nil];
  45. }

注意四点:

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

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

2.初始化时候,禁用“ok”按钮:

otherAction.enabled = NO;

self.secureTextAlertAction = otherAction;//定义一个全局变量来存储

3.当输入超过5个字符时候,使self.secureTextAlertAction = YES:

  1. - (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {
  2. UITextField *textField = notification.object;
  3.  
  4. // Enforce a minimum length of >= 5 characters for secure text alerts.
  5. self.secureTextAlertAction.enabled = textField.text.length >= 5;
  6. }

4.在“OK”action中去掉通知:

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

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

  1. - (void)showOkayCancelActionSheet {
  2. NSString *cancelButtonTitle = NSLocalizedString(@"Cancel", nil);
  3. NSString *destructiveButtonTitle = NSLocalizedString(@"OK", nil);
  4.  
  5. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  6.  
  7. // Create the actions.
  8. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  9. NSLog(@"The \"Okay/Cancel\" alert action sheet's cancel action occured.");
  10. }];
  11.  
  12. UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  13. NSLog(@"The \"Okay/Cancel\" alert action sheet's destructive action occured.");
  14. }];
  15.  
  16. // Add the actions.
  17. [alertController addAction:cancelAction];
  18. [alertController addAction:destructiveAction];
  19.  
  20. [self presentViewController:alertController animated:YES completion:nil];
  21. }

在底部显示如下:

好了,至此,基本就知道这个新的controller到底是怎样使用了。

UIActionViewController 详解 iOS8的更多相关文章

  1. UITextField使用详解

    转iOS中UITextField使用详解 (1) //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFr ...

  2. IOS中表视图(UITableView)使用详解

    IOS中UITableView使用总结 一.初始化方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styl ...

  3. iOS开发——屏幕适配篇&Masonry详解

    Masonry详解 前言 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-ip ...

  4. iOS 开发之照片框架详解

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...

  5. 《招一个靠谱的移动开发》iOS面试题及详解(下篇)

    iOS面试知识点 现在进入本篇的正题.本篇的面试题是我认为比较好的iOS开发基础知识点,希望大家看过这后在理解的基础上掌握而不是死记硬背.死记硬背很快也会忘记的. 1 iOS基础 1.1 父类实现深拷 ...

  6. 了解iOS消息推送一文就够:史上最全iOS Push技术详解

    本文作者:陈裕发, 腾讯系统测试工程师,由腾讯WeTest整理发表. 1.引言 开发iOS系统中的Push推送,通常有以下3种情况: 1)在线Push:比如QQ.微信等IM界面处于前台时,聊天消息和指 ...

  7. iOS百度地图简单使用详解

    iOS百度地图简单使用详解 百度地图 iOS SDK是一套基于iOS 5.0及以上版本设备的应用程序接口,不仅提供展示地图的基本接口,还提供POI检索.路径规划.地图标注.离线地图.定位.周边雷达等丰 ...

  8. NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY

    NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY 转载2015-07-08 18:02:02 鉴于苹果官方文档的解释 ...

  9. TableView 常用技巧与功能详解

    分割线顶格iOS8 UITableview分割线顶格的做法 //iOS8 Cell分割线顶格 if ([_tableView respondsToSelector:@selector(setSepar ...

随机推荐

  1. linux ssh scp无密码登录

    一. 应用场景 假如你Linux Client是客户端, Server为服务器,用户名为user.现在要配置从Client到Server的无密码SSH登录或者无密码的scp拷贝. 例如客户端Clien ...

  2. 英文Ubantu系统安装中文输入法

    以前都是安装的中文Ubantu,但是有时候用命令行的时候中文识别不好,会出现错误,所以这次安装了英文版,但是安装后发现输入法不好用,于是就要自己安装输入法. 安装环境为Ubantu13.04 1.卸载 ...

  3. Cocos2d-js 3.0 alp2 使用指南

    Download Cocos2d-JS: http://www.cocos2d-x.org/download Unzip and copy to C:/ Download JDK: http://ww ...

  4. W5500问题集锦(持续更新中)

    在"WIZnet杯"以太网技术竞赛中,有非常多參赛者在使用中对W5500有各种各样的疑问,对于这款WIZnet新推出的以太网芯片,使用中大家是不是也一样存在下面问题呢?来看一看: ...

  5. docker-proxy 实现容器代理访问

    可实现多个容器web主机对外提供访问 运行代理容器 nginx-proxy docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.soc ...

  6. unix进程的环境--unix环境高级编程读书笔记

    http://blog.csdn.net/xiaocainiaoshangxiao/article/category/1800937

  7. cookie 和 HttpSession

    保存会话数据的两种技术 Cookie Cookie 是客户端技术,程序把每个用户的数据以cookie的形式写给用户的浏览器.当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去.web ...

  8. 高效 jquery 的奥秘

    当你准备使用 jQuery,我强烈建议你遵循下面这些指南: 1. 缓存变量 DOM 遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $( ...

  9. MSDN无法显示该页的解决办法

    今天打开msdn,发现 查阅api时候 出现 “无法显示该页的解决办法“ 这个问题.解决方案如下: 在“运行”中输入regsvr32 "C:\Program Files\Common Fil ...

  10. Objective-C 内存管理与高级环境编程 阅读分享

    常用的调试私有API uintptr_t objc_rootRetainCount(id obj) _objc_autoreleasePoolPrint();//查看自动释放池中的对象 LLVM cl ...