iOS8推出了几个新的“controller”,主要是把类似之前的UIAlertView变成了UIAlertController,这不经意的改变,貌似把我之前理解的“controller”一下子推翻了~但是也无所谓,有新东西不怕,学会使用了就行。接下来会探讨一下这些个新的Controller。
- (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的更多相关文章

  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. WDCP是什么 关于WDCP的详细介绍

    WDCP是WDlinux Control Panel的简称,是一套用PHP开发的Linux服务器管理系统以及虚拟主机管理系统,,旨在易于使用Linux系统做为我们的网站服务器,以及平时对Linux服务 ...

  2. A Practical Guide to Distributed Scrum - 分布式Scrum的实用指南 - 读书笔记

    最近读了这本IBM出的<A Practical Guide to Distributed Scrum>(分布式Scrum的实用指南),书中的章节结构比较清楚,是针对Scrum项目进行,一个 ...

  3. the Linux Kernel: Traffic Control, Shaping and QoS

    −Table of Contents Journey to the Center of the Linux Kernel: Traffic Control, Shaping and QoS 1 Int ...

  4. careercup-链表 2.6

    2.6 给定一个有环链表,实现一个算法返回环路的开头结点. 类似leetcode中 Linked List Cycle II C++实现代码: #include<iostream> #in ...

  5. tuple类型的单词查询例子

    17.3 重写前面的TextQuery程序,使用tuple代替QueryResult类. TextQuery.h #ifndef TEXTQUERY_H #define TEXTQUERY_H #in ...

  6. 实现FTP断点续传

    应用需求: 网盘开发工作逐步进入各部分的整合阶段,当用户在客户端修改或新增加一个文件时,该文件要同步上传到服务器端对应的用户目录下,因此针对数据传输(即:上传.下载)这一块现在既定了三种传输方式,即: ...

  7. CPP: 跨平台生成GUID/UUID

    #ifndef XGUID_H#define XGUID_H #include <string>#include <stdio.h>#ifdef WIN32#include & ...

  8. XC软件管理器应用

    这是一个基于android 4.4开发的android应用-XC软件管理器.包含应用的信息查看,打开应用以及应用的卸载等功能.非常实用的一个应用,欢迎大家下载使用. 下载地址:http://downl ...

  9. runtime重写description方法打印model属性和值

    在开发过程中, 往往会有很多的model来装载属性. 而在开发期间经常会进行调试查看model里的属性值是否正确. 那么问题来了, 在objective-c里使用NSLog("%@" ...

  10. ubuntu 下编译安装 mysql php nginx 及常见错误 (持续添加)

    mysql mysql 可以使用mysql 官方提供的apt源进行安装 参见这里 php 安装前先安装一些常见库 sudo apt-get install libpng16-16 libpng16-d ...