UIAlertController 是 IOS8以后才有的
UIAlertController 继承 UIViewController
UIAlertAction 继承 NSObject
可以给UIAlertController.view 属性 添加子视图
/**
UIAlertController
A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method
UIAlertController 用来显示警告信息给用户,这个类代替 UIActionSheet 和UIAlertView
配置完 actions 和 style 通过 presentViewController:animated:completion method
*/
/**
UIAlertAction
A UIAlertAction object represents an action that can be taken when tapping a button in an alert. You use this class to configure information about a single action, including the title to display in the button, any styling information, and a handler to execute when the user taps the button. After creating an alert action object, add it to a UIAlertController object before displaying the corresponding alert to the user.
*/
/*
UIAlertActionStyleCancel
UIAlertActionStyleDefault
UIAlertActionStyleDestructive
配置时候不能配置相同的!
UIAlertController: preferredStyle:
UIAlertControllerStyleAlert
UIAlertControllerStyleActionSheet
*/
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"titile" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel_AlertAction = [UIAlertAction actionWithTitle:@"Cancel_alertAction_title" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"Cancel_alertAction");
}];
UIAlertAction *default_AlertAction = [UIAlertAction actionWithTitle:@"Default_alertAction_title" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"Default_alertAction");
NSLog(@"%@",((UITextField*)alertController.textFields[0]).text);
}];
UIAlertAction *destructive_AlertAction = [UIAlertAction actionWithTitle:@"destructive_AlertAction" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"destructive_AlertAction");
}];
//给 AlertController 配置 aciton
[alertController addAction:cancel_AlertAction];
[alertController addAction:default_AlertAction];
[alertController addAction:destructive_AlertAction];
/**
Calling this method adds an editable text field to the alert. You can call this method more than once to add additional text fields. The text fields are stacked in the resulting alert.
*/
//配置输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
NSLog(@"SecureTextEntryTextFieldWithConfigurationHandler");
}];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
NSLog(@"addTextFieldWithConfigurationHandler");
}];
//配置文本框的安全输入
((UITextField*)alertController.textFields[0]).secureTextEntry = YES;
//弹出
[self presentViewController:alertController animated:YES completion:^{
}];