1、基本使用网络
使用UIAlertController 需三步:指针
1.建立控制器并指定样式和参数.。code
2.添加按钮addAction。server
3.模态推出(底部或中间弹出)。rem
// 1. 建立alert控制器并指定style:UIAlertControllerStyleAlert||UIAlertControllerStyleActionSheet 本文以 Alert为例展现 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"肯定要操做?" message:@"操做后不可恢复" preferredStyle:UIAlertControllerStyleAlert]; // 2. 添加选项 [alertController addAction: [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"点击了取消"); }]]; [alertController addAction: [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSLog(@"点击了肯定"); }]]; // 3. 模态退出控制器 [self presentViewController:alertController animated:YES completion:nil];
*若是选项添加超过两个,会竖向排列。it
2、(举个栗子)可添加输入框,可用做输入密码等功能,也可添加多个输入框(如用户名和密码)。io
效果图:
*要对输入框中的文字进行监测,密码超过6位肯定按钮才可点。变量
由于要对AlertController的成员进行监听,而且操做肯定按钮,因此提两个全局变量。object
UIAlertAction * _otherAction; //肯定按钮 UIAlertController * _alertController; // 控制器
-(void)buttonClinck{select
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"输入密码" message:@"密码为开机密码" preferredStyle:UIAlertControllerStyleAlert]; _alertController = alertController; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.secureTextEntry = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
}]; __weak actionsheetController *Wself = self; //blcok中使用self最好用copy弱指针self,虽然有些block是局部变量(self并不包含block),但为保险,告诫本身都用。 [alertController addAction: [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { [Wself remoteNotif]; }]]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [Wself remoteNotif]; }]; [alertController addAction: otherAction]; otherAction.enabled = NO; _otherAction = otherAction; //先将肯定按钮设为不可点,在通知中当输入的密码大于n位数能够点 //推出控制器 [self presentViewController:alertController animated:YES completion:nil];
}
-(void)remoteNotif{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:_alertController.textFields.firstObject];
}
-(void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification{
UITextField *textFile = notification.object; _otherAction.enabled = textFile.text.length>5?YES:NO;
}
至此完结,注意添加监听后要移除。
文章借鉴网络,怕忘了本身总结的。不对的地方,请提出。