ios 9 以后 UIAlertController取代UIAlertView和UIActionSheet

UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet。

在UIAlertController中添加按钮和关联输入框

UIAlertAction共有三种类型,默认(UIAlertActionStyleDefault)、取消(UIAlertActionStyleCancel
)和警告(UIAlertActionStyleDestructive)。
- (void)addAction:(UIAlertAction *)action; -
(void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField
*textField))configurationHandler;
1. UIAlertControllerStyleAlert模式

UIAlertControllerStyleAlert模式会弹出一个对话框视图,点击其他区域不会退出。
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题"
message:@"这是一个测试" preferredStyle:UIAlertControllerStyleAlert]; [alert
addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入"; }]; [alert addAction:[UIAlertAction
actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:^(UIAlertAction
*action){ NSLog(@"text = %@", alert.textFields.firstObject.text);
NSLog(@"默认按钮"); }]]; [alert addAction:[UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
NSLog(@"取消按钮"); }]]; [self presentViewController:alert animated:YES
completion:^(){ NSLog(@"alert completion"); }];
显示如下

 

2. UIAlertControllerStyleActionSheet模式

UIAlertControllerStyleActionSheet模式会从底部弹出一个视图,点击其他区域也会退出。
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题"
message:@"这是一个测试" preferredStyle:UIAlertControllerStyleActionSheet]; [alert
addAction:[UIAlertAction actionWithTitle:@"选项一" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){ NSLog(@"按钮一"); }]]; [alert
addAction:[UIAlertAction actionWithTitle:@"选项二" style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action){ NSLog(@"按钮二"); }]]; [alert
addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action){ NSLog(@"取消按钮"); }]]; [self
presentViewController:alert animated:YES completion:nil];
显示如下

 

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信