IOS弹出提示框(确认/取消)

在移动开发之中,系统弹出提示框是很常见的需求,比如,账户密码输入不正确的时候,给予客户提示“输入不正确,请再次输入!“;
此文章不做详细的描述,因为这个东西的话,也很简单,如果要以其他方式实现,可以去网上找其他的文档;

一:封装的方法

- (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];
}

二:调用上面的弹出提示:

[self showError:@"用户名和密码是否为空?"];

ok,简单吧。这样实现了IOS的弹出提示框;

如果需要点击(确定,取消的按钮)提示框,两个进行选择按钮的话,可以看如下代码:
mian.h定义:

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

main.m实现:

#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];//调用弹出框

两个弹出框的案例都可行,博主就不贴图片了,复制上去就可以看效果啦!

猜你喜欢

转载自blog.csdn.net/qq_37523448/article/details/81080016