iOSUI-UIAlertController弹框控制器
从iOS8之后,系统的弹框 UIAlertView与 UIActionSheet 两个并在⼀起, 使用了⼀个新的控制器UIAlertController
样式分为两种: UIAlertControllerStyleActionSheet,UIAlertControllerStyleAlert 两种样式分别显⽰如下:
第一步:创建控制器
//Title:显示的标题 //message:标题底部显示的描述信息 //preferredStyle:弹框的样式 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定要退出嘛?" message:@“显⽰的信息" preferredStyle:UIAlertControllerStyleActionSheet];
第二步:创建按钮
/* 弹框当中的每⼀个按钮分别对应⼀个 UIAlertAction UIAlertAction创建⽅方式如下: actionWithTitle:按钮要显示的⽂字 style:按钮要显⽰的样式 样式分为三种: UIAlertActionStyleDefault:默认样式,默认按钮颜色为蓝⾊ UIAlertActionStyleCancel:设置按钮为取消.点击取消,会退出弹框. 注意:取消样式只能设置⼀个,如果有多个取消样式,则会发生错误. UIAlertActionStyleDestructive:危险操作按钮,按钮颜色显⽰为红色 */ UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { [self.navigationController popViewControllerAnimated:YES]; }];
第三步:添加按钮
//把创建的UIAlertAction添加到控制器当中. [alertController addAction:action]; [alertController addAction:action1]; //除了添加按钮之外,还可以添加⽂本框, 添加⽂本框的前提必须得要是 UIAlertControllerStyleAlert样式.否则会直接报错 添加文本框方法为: [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @“⽂本框点位文字";
}];
效果图:
第四步:显⽰弹框(相当于show操作)
[self presentViewController:alertController animated:YES completion:nil];