Objective-C 弹出提示框

一般对我来说学习一门开发语言第一个要学的就是输出和弹出提示框😩
在OC中,输出简单就是NSLOG了 下面就是弹窗

//初始化弹窗
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"输出内容" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
//弹出提示框
[self presentViewController:alert animated:true completion:nil];

可以简单的进行封装一下

- (void)showToast:(NSString *) msg {
//初始化弹窗
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:msg preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
//弹出提示框
[self presentViewController:alert animated:true completion:nil];
}

可以简单的调用上面的方法

[self showToast:@"喵喵喵!"];

上面是单一按钮的情况 如果想使用确定和取消两个按钮的话是这样

//定义确定和取消按钮
@property (strong, nonatomic) UIAlertAction *okBtn;
@property (strong, nonatomic) UIAlertAction *cancelBtn;
 - (void) showToast2 {
    // 初始化对话框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"输出内容" preferredStyle:UIAlertControllerStyleAlert];
    // 确定按钮监听
    _okBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
        NSLog(@"点击了确定按钮");
    }];
    _cancelBtn =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
        NSLog(@"点击了取消按钮");
    }];
    //添加按钮到弹出上
    [alert addAction:_okBtn];
    [alert addAction:_cancelBtn];
    // 弹出对话框
    [self presentViewController:alert animated:true completion:nil];
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容