本文主要是介绍UIAlertController的用法示例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在iOS8中 我们熟悉的UIAlertView已经不被苹果提倡了 取而代之的是UIAlertController 关于详细的解释说明上一篇转载的文章里 已经很详细的介绍了 这篇文章 主要来自己敲一下代码 顺便看一下实现的效果有什么不同
// 1.Alert
UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:nilmessage:@"确定要注销吗"preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}];
UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];
[controller addAction:okAction];
[controller addAction:cancleAction];
[selfpresentViewController:controller animated:YEScompletion:nil];
实现的效果是这样的:
// 2.ActionSheet
UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"确定要注销吗"preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}];
UIAlertAction *cancleAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];
[controller addAction:okAction];
[controller addAction:cancleAction];
[selfpresentViewController:controller animated:YEScompletion:nil];
//3.textfield
#pragma 注意 如果想加入textfield 类型只能是StyleAlert
UIAlertController *controller = [UIAlertControlleralertControllerWithTitle:@"Tips"message:@"请输入用户名密码"preferredStyle:UIAlertControllerStyleAlert];
[controller addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
textField.placeholder = @"用户名";
}];
[controller addTextFieldWithConfigurationHandler:^(UITextField *_Nonnull textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
UIAlertAction *okAction = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *_Nonnull action) {
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}];
UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];
[controller addAction:okAction];
[controller addAction:cancelAction];
[selfpresentViewController:controller animated:YEScompletion:nil];
实现效果是这样的:
最后加上对textfield用户名长度判断处理的代码
- (IBAction)logout:(UIBarButtonItem *)sender {
#pragma 注意 如果想加入textfield 类型只能是StyleAlert
controller = [UIAlertController alertControllerWithTitle:@"Tips" message:@"请输入用户名密码" preferredStyle:UIAlertControllerStyleAlert];
/*增加一个监听*/
[controller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"用户名(长度至少为3位)";
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(usernameChanged) name:UITextFieldTextDidChangeNotification object:textField];
}];
[controller addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"密码";
textField.secureTextEntry = YES;
}];
/*当确定按钮按下时 读取*/
okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[[NSNotificationCenter defaultCenter]removeObserver:self];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[self.navigationController popToRootViewControllerAnimated:YES];
}];
[controller addAction:okAction];
[controller addAction:cancelAction];
okAction.enabled = NO;
[self presentViewController:controller animated:YES completion:nil];
}
- (void)usernameChanged{
okAction.enabled = controller.textFields.firstObject.text.length >= 3;
}
长度没有3位:
如果长度达到了3位 显示效果是这样的
这篇关于UIAlertController的用法示例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!