《iOS开发笔记—自定义UIAlertController》

一、自定义UIAlertController

UIAlertController基本可以简单的理解为UIAlertController == UIAlertView + UIActionSheet

下面我们就自己来定义一个UIAlertController类。

该分类实现了遮盖住UIAlertController 的 视图。

1、UIAlertController分类的抽取

.h文件

#import <UIKit/UIKit.h>

@interface UIAlertController (category)

- (void)configTitles: (NSArray *)titles withActionHandlers:(NSArray *)actionHandlers;

@end
.m文件

#import "UIAlertController+category.h"
#import <objc/runtime.h>

static void * containerViewPorpertyKey = (void *)@"containerViewPorpertyKey";

CGFloat padding = 10;
CGFloat itemHeight = 57;
CGFloat lineHeight = 0.5;
CGFloat itemCount = 2;

@interface UIAlertController ()

@property (nonatomic, retain) UIView *containerView;

@end

@implementation UIAlertController (category)

- (id)containerView // containerView
{
    return objc_getAssociatedObject(self, containerViewPorpertyKey);
}

- (void)setContainerView:(id)containerView
{
    objc_setAssociatedObject(self, containerViewPorpertyKey, containerView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)configTitles: (NSArray *)titles withActionHandlers:(NSArray *)actionHandlers{

    // 视图
    CGFloat alertVCWidth = self.view.frame.size.width - 2 * padding;
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, alertVCWidth, itemHeight*itemCount + (itemCount-1)*lineHeight)];
    [self.view addSubview: self.containerView];

    if (titles.count-1 > 0) {
        for (int i = 0; i< titles.count-1; i++) {

            UILabel *l = [[UILabel alloc] init];
            l.frame = CGRectMake(padding, (itemHeight+lineHeight)*i, alertVCWidth - 2 * padding, itemHeight);
            l.backgroundColor = [UIColor clearColor];
            l.text = titles[i];
            l.font = [UIFont systemFontOfSize:30];
            l.textAlignment = NSTextAlignmentCenter;
            l.textColor = [UIColor blackColor];
            l.userInteractionEnabled = false;
            [self.containerView addSubview: l];
        }
    }


    // actions
    if (actionHandlers.count-1 > 0) {
        for (int i = 0; i< actionHandlers.count-1; i++) {

            UIAlertAction *action = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleDefault handler: actionHandlers[i]];
            [self addAction: action];
        }
    }

    // 取消
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:titles[titles.count-1] style:UIAlertActionStyleCancel handler: actionHandlers[actionHandlers.count-1]];
    [self addAction:cancelAction];

}

@end

2、分类使用

#import "ViewController.h"
#import "UIAlertController+category.h"

@interface ViewController ()

@property(nonatomic, retain) UIAlertController *alertVC;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    NSArray *titles = @[@"保存", @"保存222", @"123", @"取消"];
    NSArray *actionHandles = @[

                               ^(UIAlertAction *action){
                                   NSLog(@"1");
                               },

                                ^(UIAlertAction *action){
                                    NSLog(@"2");
                                },

                                ^(UIAlertAction *action){
                                    NSLog(@"3");
                                },

                                ^(UIAlertAction *action){
                                    NSLog(@"取消");
                                }
                                ];


    [self.alertVC configTitles:titles withActionHandlers:actionHandles];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self presentViewController:self.alertVC animated:YES completion:nil];
}
@end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值