OC 简单封装UIAlertController实现单按钮弹窗和双按钮弹窗

1.代码如下:

@interface AlertView : UIView
+ (AlertView *)shareAlertView;
/**带有单按钮的弹出视图 颜色可以不填*/
- (void)alertWithTitle:(NSString *)title message:(NSString *)message btnTitle:(NSString *)btnTitle btnTitleColor:(nullable UIColor *)btnTitleColor completion:(void(^)(void))completion;
/**带有双按钮的弹出视图 颜色可以不填*/
- (void)alertWIthTitle:(NSString *)title message:(NSString *)message leftBtnTitle:(NSString *)leftBtnTitle leftBtnTitleColor:(nullable UIColor *)leftBtnTitleColor leftBtnClicked:(void(^)(void))leftBtnClicked rightBtnTitle:(NSString *)rightBtnTitle rightBtnTitleColor:(nullable UIColor *)rightBtnTitleColor rightBtnClicked:(void(^)(void))rightBtnClicked;
@end
#import "AlertView.h"

@implementation AlertView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+ (AlertView *)shareAlertView {
    static AlertView *view = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        view = [[AlertView alloc]init];
        
    });
    return view;
}
- (void)alertWithTitle:(NSString *)title message:(NSString *)message btnTitle:(nonnull NSString *)btnTitle btnTitleColor:(nullable UIColor*)btnTitleColor  completion:(void (^)(void))completion {
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:btnTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        completion();
    }];
    if (btnTitleColor) {
        [action setValue:btnTitleColor  forKey:@"_titleTextColor"];
    }
    [alertC addAction:action];
    [[AlertView shareAlertView].viewController presentViewController:alertC animated:YES completion:nil];
}
- (void)alertWIthTitle:(NSString *)title message:(NSString *)message leftBtnTitle:(NSString *)leftBtnTitle leftBtnTitleColor:(nullable UIColor *)leftBtnTitleColor leftBtnClicked:(void (^)(void))leftBtnClicked rightBtnTitle:(NSString *)rightBtnTitle rightBtnTitleColor:(nullable UIColor *)rightBtnTitleColor rightBtnClicked:(void (^)(void))rightBtnClicked {
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:leftBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        leftBtnClicked();
    }];
    if (leftBtnTitleColor) {
        [action1 setValue:leftBtnTitleColor forKey:@"_titleTextColor"];
    }
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:rightBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        rightBtnClicked();
    }];
    if (rightBtnTitleColor) {
        [action2 setValue:rightBtnTitleColor forKey:@"_titleTextColor"];
    }
    
    [alertC addAction:action1];
    [alertC addAction:action2];
    
    [[AlertView shareAlertView].viewController presentViewController:alertC animated:YES completion:nil];
}
#pragma 获取当前的视图控制器
- (UIViewController *)viewController {
    UIViewController *vc = [[UIApplication sharedApplication].keyWindow rootViewController];
    return vc;
}
@end

2.使用:

[[AlertView shareAlertView]alertWithTitle:@"提示" message:@"单按钮提示框" btnTitle:@"确定" btnTitleColor:[UIColor orangeColor] completion:^{
                NSLog(@"按钮被点击了");
            }];
[[AlertView shareAlertView]alertWIthTitle:@"提示" message:@"双按钮提示" leftBtnTitle:@"取消" leftBtnTitleColor:[UIColor redColor] leftBtnClicked:^{
                NSLog(@"左按钮点击了");
            } rightBtnTitle:@"确定" rightBtnTitleColor:nil rightBtnClicked:^{
                NSLog(@"右按钮点击了");
            }];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,iOS项目中弹窗一般可以使用UIAlertController类来实现,下面是具体的步骤: 1. 创建一个UIAlertController对象,并设置弹窗的标题、消息和样式: ``` UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除吗?" preferredStyle:UIAlertControllerStyleAlert]; ``` 2. 创建UIAlertAction对象,设置按钮的标题和样式,并为按钮添加点击事件: ``` UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 点击确定按钮后执行的代码 }]; ``` 3. 将UIAlertAction对象添加到UIAlertController对象中: ``` [alert addAction:action]; ``` 4. 如果需要添加多个按钮,重复步骤2和步骤3即可。 5. 最后,使用presentViewController:animated:completion:方法显示弹窗: ``` [self presentViewController:alert animated:YES completion:nil]; ``` 完整的代码示例: ``` UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除吗?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // 点击确定按钮后执行的代码 }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:action]; [alert addAction:cancelAction]; [self presentViewController:alert animated:YES completion:nil]; ``` 这样就可以在iOS项目中实现点击按钮弹窗的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值