开发过程中常会遇到弹框,来与用户交互,下面是对UIAlertController的小小总结:
一、UIAlertController有两种样式
普通弹框 和 ActionSheet
二、创建弹框
1.普通弹框Alert
//1.创建UIAlertControler
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是一些信息" preferredStyle:UIAlertControllerStyleAlert];
/*
参数说明:
Title:弹框的标题
message:弹框的消息内容
preferredStyle:弹框样式:UIAlertControllerStyleAlert
*/
//2.添加按钮动作
//2.1 确认按钮
UIAlertAction *conform = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了确认按钮");
}];
//2.2 取消按钮
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消按钮");
}];
//2.3 还可以添加文本框 通过 alert.textFields.firstObject 获得该文本框
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请填写您的反馈信息";
}];
//3.将动作按钮 添加到控制器中
[alert addAction:conform];
[alert addAction:cancel];
//4.显示弹框
[self presentViewController:alert animated:YES completion:nil];
2.ActionSheet
//1.创建Controller
UIAlertController *alertSheet = [UIAlertController alertControllerWithTitle:@"标题" message:@"一些信息" preferredStyle:UIAlertControllerStyleActionSheet];
/*
参数说明:
Title:弹框的标题
message:弹框的消息内容
preferredStyle:弹框样式:UIAlertControllerStyleActionSheet
*/
//2.添加按钮动作
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"项目1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了项目1");
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"项目2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了项目2");
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消");
}];
//3.添加动作
[alertSheet addAction:action1];
[alertSheet addAction:action2];
[alertSheet addAction:cancel];
//4.显示sheet
[self presentViewController:alertSheet animated:YES completion:nil];