iOS---弹出提示对话框

1、就一个选项的对话框atom

代码块

#pragma mark - 封装弹出对话框方法
// 提示错误信息
- (void)showError:(NSString *)errorMsg {
    // 1.弹框提醒
    // 初始化对话框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:errorMsg preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:nil]];
    // 弹出对话框
    [self presentViewController:alert animated:true completion:nil];
}

  

须要调用弹出对话框方法的地方使用的代码以下:spa

代码块

// 弹出“请检查用户名和密码是否为空!”对话框 [self showError:@"请检查用户名和密码是否为空!"];

效果如图所示: 
这里写图片描述code

2、若是是要作两个选项的对话框 
先在.h文件中定义以下:orm

@property (strong, nonatomic) UIAlertAction *okAction; @property (strong, nonatomic) UIAlertAction *cancelAction;

而后在.m文件中写入以下代码:blog

#pragma mark - 注销:弹出对话框
- (void) logout {
    // 初始化对话框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认注销吗?" preferredStyle:UIAlertControllerStyleAlert];
    // 肯定注销
    _okAction = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        // 1.清除用户名、密码的存储

        // 2.跳转到登陆界面
        [self performSegueWithIdentifier:@"Logout" sender:nil];
    }];
    _cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    [alert addAction:_okAction];
    [alert addAction:_cancelAction];

    // 弹出对话框
    [self presentViewController:alert animated:true completion:nil];
}

 

须要调用弹出对话框方法的地方使用的代码以下:图片

代码块

// 弹出“确认注销吗?”对话框 [self logout]; 

效果如图所示: string

这里写图片描述

相关文章
相关标签/搜索