UIAlertController是苹果在iOS8里新推出的一个玩意。它把之前我们用来现实提示框的UIAlertView和UIAlertAction集成在一起了,而且不论在iPhone还是iPad上都能统一使用啦。
从UIAlertController的定义可以发现
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
它是一个UIViewControll,就是说你面前的这个在屏幕中间或是屏幕下方显示的小窗口是一个控制器了,不在是一个UIView。一段简洁的代码来说明它的基本使用
1 UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"new alert" message:@"this is a alert viewController" preferredStyle:UIAlertControllerStyleAlert]; 2 3 4 [alert addAction:[UIAlertAction actionWithTitle:@"submit" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { 5 NSLog(@"click submit button"); 6 7 NSLog(@"first textField's text is %@,the second textField's text is %@",[alert.textFields.firstObject text],[alert.textFields.lastObject text]); 8 }]]; 9 [alert addAction:[UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { 10 NSLog(@"click cancel button"); 11 }]]; 12 [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 13 14 textField.textColor=[UIColor orangeColor]; 15 }]; 16 [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) { 17 textField.secureTextEntry=YES; 18 }]; 19 20 //现实控制器 首先想到的是modal presentViewController方法 21 [self presentViewController:alert animated:YES completion:nil];