UIAlertController

也是个弹出窗口,它替代了UIActionSheet

自iOS 8开始,Apple用继承自UIViewController的UIAlertController取代了UIAlertView和UIAlertSheet。

警报控制器(UIAlertController)虽然有警告框和操作表两种形式,但其创建步骤是一样的。如下所示:

创建UIAlertController,指定警报控制器样式。
向警报控制器添加按钮。
显示UIAlertController。

2.1 创建警报控制器

创建UIAlertController非常简单,不需要设置代理、不需要指定按钮。

下面先在showAlertView:方法中,创建UIAlertController。

  • (IBAction)showAlertView:(UIButton *)sender
    {
    // 1.创建UIAlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Alert Title"
    message:@"The message is ..."
    preferredStyle:UIAlertControllerStyleAlert];
    }
    这里的preferredStyle:参数有UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet两种,这里我们要创建的是Alert View,所以使用第一种。

2.2 添加按钮

使用actionWithTitle: style: handler:方法创建UIAlertAction对象,之后把对象添加到警报控制器。

UIAlertAction对象由标题、样式和用户单击该按钮时运行的代码块三部分组成。UIAlertActionStyle有三种样式,样式一UIAlertActionStyleCancel,用于取消操作、不作任何修改,就是常见的取消按钮;样式二UIAlertActionStyleDefault,按钮的默认样式;第三种是UIAlertActionStyleDestructive,用于对数据进行更改或删除的操作,这种样式的按钮标题会自动使用红色显示。

// 初始化 添加 提示内容
UIAlertController alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"messgae" preferredStyle:UIAlertControllerStyleAlert];
/

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
UIAlertControllerStyleActionSheet = 0,// 不能加输入框,其他一样
UIAlertControllerStyleAlert // 可以添加输入框
} 弹出类型;
*/

// 添加 AlertAction 事件回调(三种类型:默认,取消,警告)
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"ok");
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"cancel");
}];
UIAlertAction *errorAction = [UIAlertAction actionWithTitle:@"error" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"error");
}];

// cancel类自动变成最后一个,警告类推荐放上面
[alertController addAction:errorAction];
[alertController addAction:okAction];
[alertController addAction:cancelAction];

// 出现
[self presentViewController:alertController animated:YES completion:^{
    NSLog(@"presented");
}];


// 移除
[alertController dismissViewControllerAnimated:YES completion:^{
    NSLog(@"dismiss");
}];

新版 带输入的 Alert

// AlertController 直接添加 textField 即可!
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    textField.placeholder = @"name";
}];

// 添加 action,再其回调中可以处理输入内容
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    // 获取上面的输入框
    UITextField *tempField = [alertController.textFields firstObject];
    NSLog(@"%@",tempField.text);
    NSLog(@"ok");
}];

新版 ActionSheet
// 提示内容 初始化 AlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"messgae" preferredStyle:UIAlertControllerStyleActionSheet];

//其余的完全一致,只是添加 TextField  会报错而已。

你可能感兴趣的:(UIAlertController)