UIAlertController是在iOS8之后才出現(xiàn)的,用于代替以前的UIAlertView
UIAlertController還有一個(gè)作用是用來制作輸入框
UIAlertController制作的輸入框.png
基本用法如下
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:
UIAlertControllerStyleAlert];
// 添加輸入框 (注意:在UIAlertControllerStyleActionSheet樣式下是不能添加下面這行代碼的)
[alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"請輸入密碼";
}];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確認(rèn)" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
// 通過數(shù)組拿到textTF的值
NSLog(@"ok, %@", [[alertVc textFields] objectAtIndex:0].text);
}];
UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
// 添加行為
[alertVc addAction:action2];
[alertVc addAction:action1];
[self presentViewController:alertVc animated:YES completion:nil];
如果使用UIAlertControllerStyleActionSheet的話囚玫,效果如下
UIAlertControllerStyleActionSheet的效果.png