iOS---弹出提示对话框的封装

这篇博客介绍了如何在iOS应用中封装并使用弹出提示对话框,包括单选项和双选项的实现方式,提供了相应的代码示例,并展示了实际效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iOS—弹出提示对话框的封装

一、就一个选项的对话框

代码块

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

需要调用弹出对话框方法的地方使用的代码如下:

代码块

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

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

二、如果是要做两个选项的对话框
先在.h文件中定义如下:

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

然后在.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];

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

更多JAVA、Unity3D的文章,请点击:

http://blog.youkuaiyun.com/u010841622

系统的UIAlert不太好用,写起来感觉太零散. 使用方法:case 0: [LTAlertView showTitle:@"LTAlert" message:@"我是普通alert" ButtonTitles:@[@"确认",@"取消"] OnTapBlock:^(LTAlertView* alert,NSInteger num) { NSLog(@"点击了第%d个按钮",num); }]; break; case 1: [LTAlertView showConfigBlock:^(LTAlertView *alertView) { alertView.alertViewStyle = UIAlertViewStyleSecureTextInput; } Title:@"LTAlert" message:@"带密码输的alert" ButtonTitles:@[@"确定",@"取消"] OnTapBlock:^(LTAlertView* alert,NSInteger num) { NSString* str = [alert textFieldAtIndex:0].text; NSLog(@"输入的文字是%@,点击了第%d个按钮",str,num); }]; break; case 2: [LTAlertView showConfigBlock:^(LTAlertView *alertView) { alertView.alertViewStyle = UIAlertViewStylePlainTextInput; } Title:@"LTAlert" message:@"带输入的alert" ButtonTitles:@[@"确定",@"取消"] OnTapBlock:^(LTAlertView* alert,NSInteger num) { NSString* str = [alert textFieldAtIndex:0].text; NSLog(@"输入的文字是%@,点击了第%d个按钮",str,num); }]; break; case 3: [LTAlertView showConfigBlock:^(LTAlertView *alertView) { alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; alertView.backgroundColor = [UIColor redColor]; } Title:@"LTAlert" message:@"弹出带输入的alert" ButtonTitles:@[@"确定",@"取消"] OnTapBlock:^(LTAlertView* alert,NSInteger num) { NSString* accountStr = [alert textFieldAtIndex:0].text; NSString* passwordStr =[alert textFieldAtIndex:1].text; NSLog(@"账号是%@,密码是%@",accountStr,passwordStr); }]; break;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值