UIAlertController

目录:
1、UIAlertControllerStyleAlert使用简介
2、UIAlertControllerStyleAlert方法集成
3、UIAlertControllerStyleActionSheet使用简介
4、UIAlertControllerStyleActionSheet方法集成

1、UIAlertControllerStyleAlert使用简介

UIAlertController_第1张图片
第一种形式:UIAlertControllerStyleAlert

实现代码:

UIAlertController * alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"APP好使不?快去评论吧。" preferredStyle:UIAlertControllerStyleAlert];
    //创建action
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"残忍拒绝" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"用户拒绝了。");
    }];
    UIAlertAction * ok =[UIAlertAction actionWithTitle:@"现在就去" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"要去评论了。");
        //获取用户输入的信息
        UITextField * account = alertCon.textFields.firstObject;
        UITextField * password = alertCon.textFields.lastObject;
        NSString * acc = account.text;
        //在此block中验证帐户是否正确,实现页面跳转。
        NSLog(@"acc = %@",acc);
        NSLog(@"密码是=====%@",password.text);
    }];

    //添加到controller上
    [alertCon addAction:cancel];
    [alertCon addAction:ok];

    //模拟appStore输入账号和密码
    [alertCon addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入账号";
    }];
    [alertCon addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入密码";
    }];

    //弹出alertcontroller
    [self presentViewController:alertCon animated:YES completion:nil];

2、UIAlertControllerStyleAlert方法集成

/**
 展示提示框(此方法是自己的.m文件中使用方法,写到BaseView中需适当更改)
 
 @param message 提示内容
 @param cancel 取消按钮标题
 @param other 其他按钮标题
 @param callback 返回被点击按钮的字符串
 */
- (void)showAlertmessage:(NSString *)message cancel:(NSString *)cancel other:(NSString *)other block:(void (^)(NSString *string))callback
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    
    //添加取消
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action)
    {
        if (callback)
        {
            callback(cancel);
        }
    }];
    
    //添加其他
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
    {
        if (callback)
        {
            callback(other);
        }
    }];
    
    [alert addAction:cancelAction];
    [alert addAction:otherAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

3、UIAlertControllerStyleActionSheet使用简介

UIAlertController_第2张图片
第二种形式:UIAlertControllerStyleActionSheet

实现代码:

UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"" message:@"请选择图片来源" preferredStyle:0];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:1 handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
    }];
    UIAlertAction * ok = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"要打开相册了");
    }];
    UIAlertAction * camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"打开相机");
    }];
    [alertController addAction:cancel];
    [alertController addAction:ok];
    [alertController addAction:camera];
    [self presentViewController:alertController animated:YES completion:^{
        NSLog(@"弹出了");
    }];

4、UIAlertControllerStyleActionSheet方法集成

/**
 UIActionSheet(需要传入要展示内容的数组,此方法是BaseView中的共用方法)
 
 @param title 要展示的标题
 @param message 要展示的内容
 @param items 除取消外其他按钮的数组
 @param superVC 要展示的ViewController
 @param callback 返回被点击的index值
 */
+ (void)showActionSheetWithTitle:(NSString *)title message:(NSString *)message itemsArray:(NSArray *)items superVC:(UIViewController *)superVC block:(void (^)(NSInteger))callback
{
    UIAlertController *alertSheet = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertSheet addAction:cancel];
    
    for (int i = 0; i < items.count; i ++){
        UIAlertAction *action = [UIAlertAction actionWithTitle:items[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (callback){
                callback(i);
            }
        }];
        
        [alertSheet addAction:action];
    }
    //展示
    if (!superVC || ![superVC isKindOfClass:[UIViewController class]]){
        superVC = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    }
    
    [superVC presentViewController:alertSheet animated:YES completion:nil];
}

你可能感兴趣的:(UIAlertController)