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 ...
随机推荐
- strace 解决库依赖问题
解决库依赖问题 starce 的另一个用处是解决和动态库相关的问题.当对一个可执行文件运行ldd时,它会告诉你程序使用的动态库和找到动态库的位置.但是如果你正在使用一个比较老 的glibc版本(2.2 ...
- JS 学习(四)对象
对象 在JS中,对象是数据(变量),拥有属性和方法. JS中所有事物都是对象:字符串.数字.数组.日期等. 对象是拥有属性和方法的特殊数据类型. 属性是与对象相关的值. 方法是能够在对象上执行的动作. ...
- PHP左、右、内连接
left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录.right join :右连接,返回右表中所有的记录以及左表中连接字段相等的记录.inner join: 内连接,又 ...
- 安卓仿照QQ工单数实现
1.使用BadgeView 控件,可以在网上下载源码或者Jar包 2.使用方法 holder.badgeView = new BadgeView(holder.item_layout.getConte ...
- Mysql 关键字及保留字
Table 10.2 Keywords and Reserved Words in MySQL 5.7 ACCESSIBLE (R) ACCOUNT[a] ACTION ADD (R) AFTER A ...
- 我现在的vimrc配置文件
runtime! debian.vim "设置编码 set encoding=utf- set fencs=utf-,ucs-bom,shift-jis,gb18030,gbk,gb2312 ...
- win10删除导航栏文档等图标,去除快捷方式
去除快捷方式字样 找到[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer],在右侧窗格新建或修改名为“link” ...
- 跨境B2B网站
大家都在谈跨境电商,其实现在比较火的应该是跨境B2B网站,它被很多的业内人士所看好,并且也取得了很喜人的成绩,无论是经营方面还是品牌打造,都从多方向带动了行业的发展. 跨境B2B网站 一.从买方市场向 ...
- 对象化前端表单(Form)提交
很常见的业务场景,就是前端一个表单,submit给后台,在web.form时代,有from 的runat="server" 配合submit 自动会提交给服务端,然后服务端解析Re ...
- SQL Server执行计划的理解
详细看:http://www.cnblogs.com/kissdodog/p/3160560.html 自己总结: 扫描Scan:逐行遍历数据. 查找Seek:根据查询条件,定位到索引的局部位置,然后 ...