準(zhǔn)備工作(以下代碼不用寫)
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) {
// 停止監(jiān)聽文本改變通知
[[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:alertController.textFields.lastObject];
// 打印第一個文本框的內(nèi)容
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;// 以圓點格式顯示
// 設(shè)置監(jiān)聽
[[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];