博客
关于我
iOS UIAlertController
阅读量:431 次
发布时间:2019-03-06

本文共 4655 字,大约阅读时间需要 15 分钟。

在Xcode的iOS9.0 SDK中,UIAlertView和UIActionSheet已被UIAlertController取代。这个替代不仅仅是为了统一使用新的控制器,更是为了提供更丰富的功能和更便捷的使用体验。以下是UIAlertController的主要使用方法和功能示例。

1. 创建对话框

要创建一个简单的对话框,可以使用UIAlertControllerStyleAlert样式:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alertController animated:YES completion:nil];

与之前的UIAlertView相比,UIAlertController无需设置代理,也不需要在初始化时指定按钮。你只需关注第三个参数,即选择AlertAction SheetCustom Alert等样式。

2. 添加动作按钮

通过创建UIAlertAction实例,你可以在控制器中添加动作按钮。每个动作按钮可以有三个样式:默认取消警示(destructive)。以下是一个简单的示例:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

3. 使用警示样式

如果需要使用警示样式,可以通过UIAlertActionStyleDestructive实现:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:resetAction];
[self presentViewController:alertController animated:YES completion:nil];

需要注意的是,动作按钮的添加顺序会影响显示顺序。

4. 创建文本对话框

如果需要添加输入框,可以使用addTextFieldWithConfigurationHandler方法来自定义文本框:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登录" message:@"请输入您的登录信息" preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"登录名";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
UITextField *username = alertController.textFields.firstObject;
UITextField *password = alertController.textFields.lastObject;
NSLog(@"%@", username.text);
NSLog(@"%@", password.text);
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];

为了实现输入验证,可以在textFieldDidChange:方法中添加验证逻辑:

-(void)textFieldDidChange:(NSNotification *)notification {
UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
if (alertController) {
UITextField *loginField = alertController.textFields.firstObject;
UIAlertAction *okAction = alertController.actions.lastObject;
okAction.enabled = (loginField.text.length > 2);
}
}

5. 使用上拉菜单

如果需要使用上拉菜单样式,可以使用UIAlertControllerStyleActionSheet

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存或删除数据" message:@"删除数据将不可恢复" preferredStyle: UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"点击了取消");
}];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"点击了删除");
}];
UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"点击了保存");
}];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAction];
[self presentViewController:alertController animated:YES completion:nil];

通过这些方法,你可以轻松地在iOS应用中使用新的UIAlertController,替代之前的UIAlertView和UIActionSheet。

转载地址:http://mhfyz.baihongyu.com/

你可能感兴趣的文章
Objective-C实现rabin-karp算法(附完整源码)
查看>>
Objective-C实现radians弧度制算法(附完整源码)
查看>>
Objective-C实现radianToDegree弧度到度算法(附完整源码)
查看>>
Objective-C实现radix sort基数排序算法(附完整源码)
查看>>
Objective-C实现rail fence围栏密码算法(附完整源码)
查看>>
Objective-C实现randomized heap随机堆算法(附完整源码)
查看>>
Objective-C实现rayleigh quotient瑞利商算法(附完整源码)
查看>>
Objective-C实现RC4加解密算法(附完整源码)
查看>>
Objective-C实现RC4加解密算法(附完整源码)
查看>>
Objective-C实现recursive bubble sor递归冒泡排序算法(附完整源码)
查看>>
Objective-C实现recursive insertion sort递归插入排序算法(附完整源码)
查看>>
Objective-C实现recursive quick sort递归快速排序算法(附完整源码)
查看>>
Objective-C实现RedBlackTree红黑树算法(附完整源码)
查看>>
Objective-C实现redis分布式锁(附完整源码)
查看>>
Objective-C实现regular-expression-matching正则表达式匹配算法(附完整源码)
查看>>
Objective-C实现relu线性整流函数算法(附完整源码)
查看>>
Objective-C实现restful api服务(附完整源码)
查看>>
Objective-C实现reverse letters反向字母算法(附完整源码)
查看>>
Objective-C实现ReverseNumber反转数字算法 (附完整源码)
查看>>
Objective-C实现ReversePolishNotation逆波兰表示法算法 (附完整源码)
查看>>