UIAlertViewController的使用
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的使用的更多相关文章
- UIAlerView、UIActionSheet 和UIAlertViewController(点击注销确认按钮实现)
- (IBAction)loginOut:(UIBarButtonItem *)sender { UIActionSheet *actionSheet = [[UIActionSheet alloc] ...
- iOS之改变UIAlertViewController字体的颜色
NSString *message = @"请确认信息是否正确?"; NSString *title = @"提示"; UIAlertController *a ...
- UIAlertViewController+TextField 输入框
if (IOS8) { UIAlertController *alertController=[UIAlertController alertControllerWithTitle:CustomLoc ...
- swift 学习之 UIAlertViewController
// // PushViewController.swift // tab // // Created by su on 15/12/7. // Copyright © 2015年 tian. ...
- swift学习之-- UIAlertVIewController - uiactionsheet
// // ViewController.swift // actionsheet // // Created by su on 15/12/7. // Copyright © 2015年 t ...
- swift学习之-- UIAlertViewController -alert
// // ViewController.swift // alertView // // Created by su on 15/12/7. // Copyright © 2015年 tia ...
- UIAlertViewController 2
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...
- UIAlertView, UIAlertViewController
iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController 在实现视图控制器间的过渡动画效果和自适应设备尺 ...
- 使用MonoTouch.Dialog简化iOS界面开发
MonoTouch.Dialog简称MT.D,是Xamarin.iOS的一个RAD工具包.它提供易于使用的声明式API,不需要使用导航控制器.表格等ViewController来定义复杂的应用程序UI ...
随机推荐
- Linux内核模块设计
内核的设计有两种方式:单内核和微内核,两者各有优劣,关于两者的比较可以参见wiki.windowds和Solaris采用微内核结构. Linux内核采用单内核结构,设计比较简单,但单内核的理念是把所有 ...
- URL特殊字符的转义
+ 转义符为 %2B 空格 转义符为 + 或 %20 / 转义符为 %2F ? 转义符为 %3F % 转义符为 %25 # 转义符为 %23 & 转义符为 %26 = 转义符为 %3D
- JUC.Condition学习笔记[附详细源码解析]
目录 Condition的概念 大体实现流程 I.初始化状态 II.await()操作 III.signal()操作 3个主要方法 Condition的数据结构 线程何时阻塞和释放 await()方法 ...
- Word设置首页不同
Sub 批量格式设置() '此代码为指定文件夹中所有选取的WORD文件的进行格式设置 Dim MyDialog As FileDialog, vrtSelectedItem As Variant, ...
- 解决低版本chrome浏览器不支持es6 Array.find()
if (!Array.prototype.find) { Array.prototype.find = function(predicate) { 'use strict'; if ( ...
- git不是内部命令和可执行程序解决方法
1.从Git官网下载windows版本的git:http://git-scm.com/downloads 2.一般使用默认设置即可:一路next,git安装完毕! 3.但是如果这时你打开windows ...
- jmc远程监控java服务
简介 JMC(Java mission control)是JDK自带的一个图形界面监控工具,监控信息非常全面.他的安装目录在%JAVA_HOME%\bin\jmc.exe 最近JSTORM程序在集群环 ...
- 微信支付开发-当前页面的URL未注册
微信支付的开发设置要求设置微信支付可以发起的目录. 当出现“当前页面的URL未注册”的错误时,检查你的授权目录(一般是未填写或填写有误),注意要二级或者三级目录完全匹配.
- Myeclipse中打开接口实现类的快捷键
Myeclipse中打开接口实现类的快捷键-----Ctrl + T Myeclipse中 Open Type快捷键-----Ctrl + Shift + T
- 我的第一个web应用开发搭建-环境配置
MyEclipse 2014 破解图文详细教程 MyEclipse作为Java EE最受欢迎的IDE,最新版本为2014版,MyEclipse 2014破解的方法. 一.安装完成MyEclipse20 ...