UIAlertViewController是苹果自带的信息提示框,仅在iOS8.0以后可以使用

NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController

下面是一个使用的简单例子:

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil];
UIAlertController的alertControllerWithTitle:message:preferredStyle:方法中有个样式参数:preferredStyle

此参数为枚举类型:

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,
UIAlertControllerStyleAlert
} NS_ENUM_AVAILABLE_IOS(8_0);
UIAlertControllerStyleAlert:该样式与UIAlertView的样式一样,显示在屏幕中央。
UIAlertControllerStyleActionSheet:该样式显示在屏幕底部,自下而上推出。
UIAlertAction的actionWithTitle:style:handler:方法中也有一个样式参数叫做:style
此参数为枚举类型:
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);

UIAlertActionStyleDefault是默认样式,白底黑字,响应touchUpInside事件。
UIAlertActionStyleCancel是取消样式,与UIAlertActionStyleDefault唯一的不同就是它的字体加粗了。
UIAlertActionStyleDestructive是具有警示性的样式,与UIAlertActionStyleDefault唯一的不同就是它的字体变为红色了。
有个问题:UIAlertController只能显示提示信息吗?如果是的话,为什么不继续使用UIAlertView?如果不是,那它还可以做什么?

答:不是。UIAlertController还可以添加文本输入框,与用户进行文本信息输入交互,而不再仅仅是输出给用户提示信息。接下来看一下一个简单的例子:

首先,将alertController声明为全局对象便于在整个类中使用
@interface ViewController ()

@property (nonatomic, strong) UIAlertController *alertController;

@end

  

接下来是实例化alertController

    self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];

    [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"登录";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
[self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
[okAction setEnabled:NO]; [self.alertController addAction:cancelAction];
[self.alertController addAction:okAction]; [self presentViewController:self.alertController animated:YES completion:nil];

  

较之前面的代码,这里多了两行代码:

    [self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"登录";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
[self.alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];

  

这两行代码就是在alertController中加入文本输入框,让用户输入必要信息,以便交互。其中给textField设置了placeholder和secureTextEntry,另外还添加了属性监听。

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];

  

此处的监听事件会在文本框变化时触发,方法如下:

-(void)alertViewTextFieldDidChange:(NSNotification *)notification
{
UITextField *textField = notification.object;
UIAlertAction *action = self.alertController.actions[1];
[action setEnabled:NO];
if(textField.text.length>3)
{
NSLog(@"textField.text = %@",textField.text);
[action setEnabled:YES];
}
}

  

方法中通过:notification.object来获取到输入框,用于后续操作。


UIAlertViewController的使用的更多相关文章

  1. UIAlerView、UIActionSheet 和UIAlertViewController(点击注销确认按钮实现)

    - (IBAction)loginOut:(UIBarButtonItem *)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] ...

  2. iOS之改变UIAlertViewController字体的颜色

    NSString *message = @"请确认信息是否正确?"; NSString *title = @"提示"; UIAlertController *a ...

  3. UIAlertViewController+TextField 输入框

    if (IOS8) { UIAlertController *alertController=[UIAlertController alertControllerWithTitle:CustomLoc ...

  4. swift 学习之 UIAlertViewController

    // //  PushViewController.swift //  tab // //  Created by su on 15/12/7. //  Copyright © 2015年 tian. ...

  5. swift学习之-- UIAlertVIewController - uiactionsheet

    // //  ViewController.swift //  actionsheet // //  Created by su on 15/12/7. //  Copyright © 2015年 t ...

  6. swift学习之-- UIAlertViewController -alert

    // //  ViewController.swift //  alertView // //  Created by su on 15/12/7. //  Copyright © 2015年 tia ...

  7. UIAlertViewController 2

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

  8. UIAlertView, UIAlertViewController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...

  9. 使用MonoTouch.Dialog简化iOS界面开发

    MonoTouch.Dialog简称MT.D,是Xamarin.iOS的一个RAD工具包.它提供易于使用的声明式API,不需要使用导航控制器.表格等ViewController来定义复杂的应用程序UI ...

随机推荐

  1. xp和win7安装telnet服务

    xp: 有些ghost版本的xp会精简掉telnet服务 首先telnet服务需要的几个文件: tlntadmn.exe tlntsess.exe tlntsvr.exe tlntsvrp.dll 文 ...

  2. 【SSM 6】Spring+SpringMVC+Mybatis框架搭建步骤

    一.整体概览 首先看maven工程的创建 二.各层的文件配置 2.1,SSM父工程 <span style="font-family:KaiTi_GB2312;font-size:18 ...

  3. [转载]Python & Selenium -- 页面加载时间过长&启动指定FF

    原文链接:https://my.oschina.net/u/2344787/blog/400507?p={{page}} 1. selenium webdriver在get方法会一直等待页面加载完毕才 ...

  4. 利用IDL将一个txt文档拆分为多个

    测试.txt文档,每47行的格式相同,通过代码每47行存为一个txt,txt文档命名为其第一行数据. 代码如下: file='G:\data\测试.txt' openr,lun,file,/Get_L ...

  5. Dev tdxDBTreeView

    可以快速的用tree展示层次结构,无需任何编码;对tree的操作会自动post到数据集:对数据集的操作会 在tree上表现 一.关键 设置 datasource displayField:节点的   ...

  6. Python自动化 【第十一篇】:Python进阶-RabbitMQ队列/Memcached/Redis

     本节内容: RabbitMQ队列 Memcached Redis 1.  RabbitMQ 安装 http://www.rabbitmq.com/install-standalone-mac.htm ...

  7. yii + elasticsearch 手册

    https://zhuowenji1.gitbooks.io/elasticsearch/content/an_zhuang_yii2.html

  8. html5+css 图片自适应

    <div style="position:absolute; width:100%; height:100%; z-index:-1; left:0; top:0;"> ...

  9. shell 脚本杀死后台由php脚本控制运行的所有php脚本和java程序

    效果: 运行命令: ./killallphpjavarm.sh java 源码: #!/bin/sh#根据进程名杀死进程#FileName: killjavaphprm.sh pgrep php ki ...

  10. Python:利用内建函数将字符串转化为整数

    使用内建函数raw_input()内建函数,它读取标准输入,并将读取到的数据赋值给指定的变量.我们可以使用int()内建函数将用户输入的字符串转换为整数: >>> user = ra ...