免费开源的iOS开发学习平台

提醒功能实现:UIAlertController与UIAlertAction

苹果自iOS8开始,就已经废弃了之前用于界面提醒的UIAlertView类以及UIActionSheet,取而代之的是UIAlertController以及UIAlertAction,从实际使用情况来看,苹果把不同类型/样式提醒的实现方法进行了统一,简化了有关提醒功能的实现。

UIAlertController的使用

UIAlertController是用于管理和控制某个提醒的类,通过UIAlertController类的对象可以实现提醒样式的定制。UIAlertController类中有如下常用的方法。

  • UIAlertController实例化方法,通过该方法可以创建一个UIAlertController类的对象
+ (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(UIAlertControllerStyle)preferredStyle;
  • 在提醒对象中添加按钮,每个按钮都是一个UIAlertAction类的对象
- (void)addAction:(UIAlertAction *)action;
  • 在界面上显示提醒
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion;

在iOS开发中,提醒主要有两种样式,一种是显示在屏幕中央的,另外一种是显示在屏幕底部的。在UIAlertController类中,对提醒的样式进行了统一,使用preferredStyle来区分。

typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
    UIAlertControllerStyleActionSheet = 0,
    UIAlertControllerStyleAlert
}

UIAlertAction的使用

UIAlertAction是定义提醒中每个按钮的样式以及用户点击后所执行的操作,每个UIAlertAction对象都需要添加到一个UIAlertController对象上才能生效。当我们需要创建UIAlertAction对象时,可以使用如下的类方法。

+ (instancetype)actionWithTitle:(nullable NSString *)title style:(UIAlertActionStyle)style handler:(void (^ __nullable)(UIAlertAction *action))handler;

每个提醒的按钮样式也是可以定制的,例如红色按钮、加粗字体按钮等。提醒按钮的样式是通过UIAlertActionStyle参数配置的。

typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
    UIAlertActionStyleDefault = 0,
    UIAlertActionStyleCancel,
    UIAlertActionStyleDestructive
} NS_ENUM_AVAILABLE_IOS(8_0);

示例代码

下方的示例代码中创建了一个弹出式的提醒,并且包含了两个按钮,当点击每个按钮后都会执行特定的动作。首先创建一个Single View Application,然后在ViewController.m中添加如下代码,注意需要在viewDidAppear:方法中实现。

-(void)viewDidAppear:(BOOL)animated{
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"                                                                     message:@"是否要访问99iOS?"                                                                preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"YES"
                                                            style:UIAlertActionStyleDefault                                                              handler:^(UIAlertAction * action) {
                                                                NSLog(@"点击了YES");
                                                            }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"NO"
                                                           style:UIAlertActionStyleCancel                                                     handler:^(UIAlertAction *action) {
                                                               NSLog(@"点击了NO");
                                                           }];
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}

当应用启动后,会弹出一个提醒框,包括两个按钮。

当点击YES时,会打印如下日志,即执行YES按钮对应的UIAlertAction中设置的代码。

https://github.com/99ios/7.6