UIAlertController
2016-07-05 10:09
246 查看
苹果希望通过一个storyboard搞定适配所有屏幕的显示 将所有屏幕尺寸都抽象成正方形
打开 ViewController.m
加入方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 危险操作:弹框提醒
// 1.UIAlertView
// 2.UIActionSheet
// iOS8开始:UIAlertController == UIAlertView + UIActionSheet
[self alertView];
}
- (void)alertView
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"电量不足,请尽快充电" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
}
运行
点击屏幕
修改类型值 alert.alertViewStyle
总之alertView总共就是以下
typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
};
4种样式
在类中加入方法
- (void)actionSheet
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"警告:电量不足,请尽快充电" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定"
otherButtonTitles:@"关闭", nil];
[sheet showInView:self.view];
}
将 touchesBegan 方法中的
[self alertView]; 注释掉在其下加入
[self actionSheet];
运行
iOS8中,UIActiconSheet已被废弃,同时基于UIActionSheet自定义的也将无效果。
Apple将UIActionSheet和UIAlertView整合成一个接口UIAlertController。
原来的是一个view,展示在window视图之上。现在改成了controller,展示方式变成由当前的controller直接present出来。
而不再使用原先的显示方式如 [alert show] [sheet showInView:self.view]
进入 UIAlertController 源代码查看知道该类也是继承 UIViewController
表示悬浮在屏幕中间或某一块位置的控制器 即UIAlertController是UIAlertView + UIActionSheet结合体
将 touchesBegan 方法中的
[self actionSheet]; 注释掉在其下加入
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"电量不足,请尽快充电" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
运行
发现没有按钮
修改 UIAlertControllerStyleAlert 样式为 UIAlertControllerStyleActionSheet
运行
在修改回 UIAlertControllerStyleAlert 样式
通过addAction接口添加具体按钮,设置按钮title、style和使用block方式直接加入按钮响应接口。
style有以下几种:
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);
在 [self presentViewController:alert animated:YES completion:nil];
顶上加入
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了确定按钮");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击了取消按钮");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"点击了其它按钮");
}]];
运行
将
[alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"点击了其它按钮");
}]];
注释掉在其下加入
// 添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.textColor = [UIColor redColor];
textField.text = @"123";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = YES;
textField.text = @"123";
}];
运行
注意: UIAlertControllerStyleActionSheet 样式不能添加文本框
// Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert
// 只能在UIAlertControllerStyleAlert样式的view上添加文本框
将
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了确定按钮");
}]];
改为
__weak typeof(alert) weakAlert = alert;//防止循环引用
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
}]];
运行
监听文本内容改变
在
// 添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.textColor = [UIColor redColor];
textField.text = @"123";
下面加入
[textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
在类中加入方法
- (void)usernameDidChange:(UITextField *)username
{
NSLog(@"%@", username.text);
}
运行
与原来的UIActionSheet和UIAlertView比较:
1、视觉上没有改变,跟之前的样式保持一致;
2、改为controller方式后,控件的生命周期能够更好的控制;方便的弹出和收起
3、当前controller弹出UIAlertController后,再次present controller必须在UIAlertController的基础上present。present后,UIAlertController会被盖住。而之前的UIActionSheet和UIAlertView由于是add在window上面的,当在弹出他们的controller基础上再present controller的话,UIActionSheet和UIAlertView弹框仍然保留在最上方。
4、修改为controller方式后,接口更加简单,而且按钮响应方式更加明了简介,不需要在设置delegate和实现响应的回调等。
打开 ViewController.m
加入方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 危险操作:弹框提醒
// 1.UIAlertView
// 2.UIActionSheet
// iOS8开始:UIAlertController == UIAlertView + UIActionSheet
[self alertView];
}
- (void)alertView
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"电量不足,请尽快充电" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
}
运行
点击屏幕
修改类型值 alert.alertViewStyle
总之alertView总共就是以下
typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
};
4种样式
在类中加入方法
- (void)actionSheet
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"警告:电量不足,请尽快充电" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定"
otherButtonTitles:@"关闭", nil];
[sheet showInView:self.view];
}
将 touchesBegan 方法中的
[self alertView]; 注释掉在其下加入
[self actionSheet];
运行
iOS8中,UIActiconSheet已被废弃,同时基于UIActionSheet自定义的也将无效果。
Apple将UIActionSheet和UIAlertView整合成一个接口UIAlertController。
原来的是一个view,展示在window视图之上。现在改成了controller,展示方式变成由当前的controller直接present出来。
而不再使用原先的显示方式如 [alert show] [sheet showInView:self.view]
进入 UIAlertController 源代码查看知道该类也是继承 UIViewController
表示悬浮在屏幕中间或某一块位置的控制器 即UIAlertController是UIAlertView + UIActionSheet结合体
将 touchesBegan 方法中的
[self actionSheet]; 注释掉在其下加入
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"电量不足,请尽快充电" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
运行
发现没有按钮
修改 UIAlertControllerStyleAlert 样式为 UIAlertControllerStyleActionSheet
运行
在修改回 UIAlertControllerStyleAlert 样式
通过addAction接口添加具体按钮,设置按钮title、style和使用block方式直接加入按钮响应接口。
style有以下几种:
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);
在 [self presentViewController:alert animated:YES completion:nil];
顶上加入
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了确定按钮");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击了取消按钮");
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"点击了其它按钮");
}]];
运行
将
[alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"点击了其它按钮");
}]];
注释掉在其下加入
// 添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.textColor = [UIColor redColor];
textField.text = @"123";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.secureTextEntry = YES;
textField.text = @"123";
}];
运行
注意: UIAlertControllerStyleActionSheet 样式不能添加文本框
// Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert
// 只能在UIAlertControllerStyleAlert样式的view上添加文本框
将
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了确定按钮");
}]];
改为
__weak typeof(alert) weakAlert = alert;//防止循环引用
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
}]];
运行
监听文本内容改变
在
// 添加文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.textColor = [UIColor redColor];
textField.text = @"123";
下面加入
[textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
在类中加入方法
- (void)usernameDidChange:(UITextField *)username
{
NSLog(@"%@", username.text);
}
运行
与原来的UIActionSheet和UIAlertView比较:
1、视觉上没有改变,跟之前的样式保持一致;
2、改为controller方式后,控件的生命周期能够更好的控制;方便的弹出和收起
3、当前controller弹出UIAlertController后,再次present controller必须在UIAlertController的基础上present。present后,UIAlertController会被盖住。而之前的UIActionSheet和UIAlertView由于是add在window上面的,当在弹出他们的controller基础上再present controller的话,UIActionSheet和UIAlertView弹框仍然保留在最上方。
4、修改为controller方式后,接口更加简单,而且按钮响应方式更加明了简介,不需要在设置delegate和实现响应的回调等。
相关文章推荐
- UIWindow加点击手势,上面的UITableView点击失效
- git 官方文档
- ios的手势操作之UIGestureRecognizer浅析
- 如何将复选框的value值传递给服务器
- notification 更新 采用 builder构建
- JS与HTML结合使用marquee标签实现无缝滚动效果代码
- Android如何判断MIUI和魅族手机
- UnicodeDecodeError: ‘XXX’ codec can’t decode bytes in position 2-5: illegal multibyte sequence
- iOS App开发中通过UIDevice类获取设备信息的方法
- UIView的setNeedsLayout,layoutIfNeeded,layoutSubviews 方法之间的关系解释
- 动态库、竖屏显示
- Error:Execution failed for task ':clean'. > Unable to delete directory :\build\intermediates
- Autolayout在UITableView中的坑
- UITableView详解
- UIButton详解
- PowerBuilder安装及破解教程
- PAT (Advanced Level) 1101. Quick Sort (25)
- php中使用include(require)嵌套包含文件时的一些注意问题
- PAT (Advanced Level) 1099. Build A Binary Search Tree (30)
- Shortest Distance from All Buildings