您的位置:首页 > 产品设计 > UI/UE

UIKit框架-07.UIAlertController使用

2015-07-13 22:06 483 查看

1.UIAlertController概述

UIAlertController是IOS 8.0之后出现的新特性,它同时结合了UIAlertView和UIActionSheet的功能

IOS 8.0之前我们还是需要使用UIAlertView和UIActionSheet,完成弹框功能

2.使用UIAlertController实现UIAlertView效果

实现UIAlerView

-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{
// 1.创建UIAlertControllerStyleAlert样式的UIAlertController
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"标题" message:@"正文" preferredStyle:UIAlertControllerStyleAlert];
// 2.创建按钮
/*
UIAlertActionStyleDefault = 0, //默认样式
UIAlertActionStyleCancel,      // 字体加粗
UIAlertActionStyleDestructive  // 强调,字体标红
*/
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {// 点击事件
NSLog(@"action1被点击了");
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"按钮2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {// 点击事件
NSLog(@"action2被点击了");
}];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"按钮3" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {// 点击事件
NSLog(@"action3被点击了");
}];
// 2.1 添加按钮
[alertVc addAction:action1];
[alertVc addAction:action2];
[alertVc addAction:action3];
// 3.添加文本框
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
NSLog(@"文本框1");
}];
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
NSLog(@"文本框2");
textField.secureTextEntry = YES;//设置密文
}];
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
NSLog(@"文本框3");
}];

//4.添加显示控制器方法
[self presentViewController:alertVc animated:YES completion:nil];

}


效果图



注意:我们使用UIAlertView最多只能创建2个文本输入框,而实用UIAlertController可以创建多个文本输入框

3.实现UIActionSheet效果

实现UIActionSheet

-(void)setAlertview{
// 1.创建UIAlertControllerStyleAlert样式的UIAlertController
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"标题" message:@"正文" preferredStyle:UIAlertControllerStyleAlert];
// 2.创建按钮
/*
UIAlertActionStyleDefault = 0, //默认样式
UIAlertActionStyleCancel,      // 字体加粗
UIAlertActionStyleDestructive  // 强调,字体标红
*/
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {// 点击事件
NSLog(@"action1被点击了");
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"按钮2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {// 点击事件
NSLog(@"action2被点击了");
}];
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"按钮3" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {// 点击事件
NSLog(@"action3被点击了");
}];
// 2.1 添加按钮
[alertVc addAction:action1];
[alertVc addAction:action2];
[alertVc addAction:action3];
// 3.添加文本框
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
NSLog(@"文本框1");
}];
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
NSLog(@"文本框2");
textField.secureTextEntry = YES;//设置密文
}];
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {
NSLog(@"文本框3");
}];

//4.添加显示控制器方法
[self presentViewController:alertVc animated:YES completion:nil];
}


效果图



注意:

如果UIAlertController的样式是ActionSheet, 就不能添加输入框

reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'


UIAlertController继承UIController类,而UIAlertView和UIActionSheet继承UIView,所以要想在视图上显示UIAlertController,需要调用如下方法

[self presentViewController:alertVc animated:YES completion:nil];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: