准备工作(以下代码不用写)
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,// 从底部弹出
UIAlertControllerStyleAlert// 从中心弹出
} NS_ENUM_AVAILABLE_IOS(8_0);
typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
UIAlertActionStyleDefault = 0,
UIAlertActionStyleCancel,
UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);
上干货
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 停止监听文本改变通知
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.lastObject];
// 打印第一个文本框的内容
NSLog(@"%@",alertController.textFields.firstObject.text);
}];
[alertController addAction:OKAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:destructiveAction];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.backgroundColor = [UIColor redColor];
textField.placeholder = @"请输用户名";
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.backgroundColor = [UIColor redColor];
textField.placeholder = @"请输入密码";
textField.secureTextEntry = YES;// 以圆点格式显示
// 设置监听
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];
}];
// 将OK按钮禁用
OKAction.enabled = NO;
self.alertAction = OKAction;//存储OK按钮
[self presentViewController:alertController animated:YES completion:nil];
从底部弹出提示框
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"OK/取消" message:@"请你登陆!" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *OKaction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:OKaction];
[alertController addAction:cancleAction];
[alertController addAction:destructiveAction];
[self presentViewController:alertController animated:YES completion:nil];