多个block合并到一个方法的使用

本文介绍了如何在iOS中通过自定义AlertView,将多个Block合并到一个方法中,以简化代理方法的使用。通过示例代码展示了如何创建带有取消和确认按钮的弹窗,并在点击时通过Block进行回调。

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

    iOS中轻量的传递方法block,在这里记录一下,以便以后查看,在这里以一个自定义的AlertView为例

先看一下代码:.h文件中

将两个按钮的点击事件以block的形式作为show方法的参数去回调,这样就不用再写代理方法调用了。

#import <UIKit/UIKit.h>


typedef void(^CancelBlock)(UIButton *button);

typedef void(^SureBlock)(UIButton *button);


@interface YiCustomAlertView : UIView


@property (nonatomic, copy) CancelBlock cancelBlock;

@property (nonatomic, copy) SureBlock sureBlock;


// show View;

- (void)showViewWithMessage:(NSString *)message

                cancelTitle:(NSString *)cancelTitle

                  sureTitle:(NSString *)sureTitle

                cancelBlock:(void(^)(UIButton *sender))cancelBlock

               confirmBlcok:(void(^)(UIButton *sender))sureBlock;


@end


在.m文件中的实现,在弹出和移除的方法里没有做相对应的动画,仅仅实现了功能。动画根据自己的情况添加

#import "YiCustomAlertView.h"


#define kDefaultBackgroundColor [UIColor clearColor]

#define kAlertColor [UIColor whiteColor]

#define kCancelBtnTextColor [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1]

#define kSureBtnTextColor [UIColor colorWithRed:1.0 green:84/255.0 blue:84/255.0 alpha:1]

#define kDefaultImage [UIImage imageNamed:@"alert_popup_shadow"]

#define kAlertViewTag 1000000

#define kMargin 10.0

#define kAlertWidth 300.0

#define kAlertHeight 150.0

#define kScreenSize [UIScreen mainScreen].bounds.size


@interface YiCustomAlertView ()


@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, strong) UIButton *sureBtn;

@property (nonatomic, strong) UILabel *msgLabel;


@end


@implementation YiCustomAlertView


- (instancetype)init {

    self = [super init];

    if (self) {

        [self setupViews];

    }

    return self;

}


- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

        [self setupViews];

    }

    return self;

}


- (instancetype)initWithCoder:(NSCoder *)aDecoder {

    self = [super initWithCoder:aDecoder];

    if (self) {

        [self setupViews];

    }

    return self;

}


- (void)setupViews {


    UIWindow *kWindow = [UIApplication sharedApplication].keyWindow;

    self.frame = kWindow.bounds;

    self.backgroundColor = [UIColor clearColor];

    [kWindow addSubview:self];


    //背景图片

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenSize.width - kAlertWidth)/2.0, (kScreenSize.height - kAlertHeight) / 2.0, kAlertWidth, kAlertHeight)];

    imgView.image = kDefaultImage;

    [self addSubview:imgView];


    //alert view

    CGFloat xOffset = imgView.frame.origin.x + kMargin;

    CGFloat yAlOffset = imgView.frame.origin.y + kMargin;

    UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake(xOffset, yAlOffset, (kAlertWidth - kMargin * 2), (kAlertHeight - kMargin * 2))];

    alertView.backgroundColor = [UIColor whiteColor];

    alertView.layer.cornerRadius = 2.0;

    alertView.layer.masksToBounds = YES;

    [self addSubview:alertView];


    //meaasge label

    CGFloat labelW = alertView.frame.size.width - 8 * 2;

    UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, labelW, 74.0)];

    msgLabel.textColor = [UIColor blackColor];

    msgLabel.numberOfLines = 0;

    msgLabel.lineBreakMode = NSLineBreakByWordWrapping;

    msgLabel.textAlignment = NSTextAlignmentCenter;

    msgLabel.font = [UIFont systemFontOfSize:14];

    msgLabel.preferredMaxLayoutWidth = labelW;

    [alertView addSubview:msgLabel];

    self.msgLabel = msgLabel;

    //创建按钮

    CGFloat btnWidth = (kAlertWidth - kMargin * 6 - 1) / 2.0;

    CGFloat btnHeight = 22.0;

    CGFloat yOffset = alertView.frame.size.height - kMargin - btnHeight;

    //取消按钮

    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    cancelBtn.frame = CGRectMake(kMargin, yOffset, btnWidth, btnHeight);

    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];

    [cancelBtn setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5]

                    forState:UIControlStateNormal];

    [cancelBtn addTarget:self

                  action:@selector(cancelAction:)

        forControlEvents:UIControlEventTouchUpInside];

    self.cancelBtn = cancelBtn;

    [alertView addSubview:cancelBtn];


    //separator

    CGFloat separatorH = 17.0;

    CGFloat yoffset = cancelBtn.frame.origin.y + (btnHeight - separatorH) / 2.0;

    UIView *separator = [[UIView alloc] initWithFrame:CGRectMake((kMargin * 2 + btnWidth), yoffset, 1.0, separatorH)];

    separator.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];

    [alertView addSubview:separator];


    //确定按钮

    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    sureBtn.frame = CGRectMake(kMargin * 3 + btnWidth + 1, yOffset, btnWidth, btnHeight);

    sureBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];

    [sureBtn setTitleColor:[UIColor redColor]

                  forState:UIControlStateNormal];

    [sureBtn addTarget:self

                action:@selector(sureAction:)

      forControlEvents:UIControlEventTouchUpInside];

    self.sureBtn = sureBtn;

    [alertView addSubview:sureBtn];

    

}


- (void)showViewWithMessage:(NSString *)message

                cancelTitle:(NSString *)cancelTitle

                  sureTitle:(NSString *)sureTitle

                cancelBlock:(void(^)(UIButton *sender))cancelBlock

               confirmBlcok:(void(^)(UIButton *sender))sureBlock

{

    self.msgLabel.text = message;

    [self.cancelBtn setTitle:cancelTitle

                    forState:UIControlStateNormal];

    [self.sureBtn setTitle:sureTitle

                  forState:UIControlStateNormal];

    self.cancelBlock = cancelBlock;

    self.sureBlock = sureBlock;

}


#pragma mark - Action Button

- (void)cancelAction:(UIButton *)sender {

    if (self.cancelBlock) {

        self.cancelBlock(sender);

    }

    [self removeFromSuperview];

}


- (void)sureAction:(UIButton *)sender {

    if (self.sureBlock) {

        self.sureBlock(sender);

    }

    [self removeFromSuperview];

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值