iOS 枚举+block的基础使用

  • B传递给A(A拥有B),则B.h --->
#import 

typedef void(^JHBannerViewBlock)(NSInteger tag);

typedef enum : NSInteger {
    ///优惠打折
    JHBannerViewDiscountBtnTag,
    ///品牌商家
    JHBannerViewBrandBtnTag,
    ///厂家直销
    JHBannerViewDirectBtnTag,
    ///批发直送
    JHBannerViewWholesaleBtnTag,
    ///库存尾货
    JHBannerViewInventoryBtnTag,
}JHBannerViewBtnTag;

@interface JHBannerView : UIView

/** block*/
@property (nonatomic, copy) JHBannerViewBlock block;

@end
  • B.m --->
DiscountBtn.tag = JHBannerViewDiscountBtnTag;
.......

- (void)clickBtn:(UIButton *)btn{
    if (self.block) {
        self.block(btn.tag);
    }
    
}
  • A.m --->
-(JHBannerView *)banner2{
    if (!_banner2) {
        _banner2 = [[JHBannerView alloc]initWithFrame:CGRectMake(0, TableHeaderViewHeight, SCREEN_WIDTH, banner2H)];
        _banner2.block = ^(NSInteger tag) {
            switch (tag) {
                case JHBannerViewDiscountBtnTag:
                    NSLog(@"跳转优惠折扣");
                    break;
                case JHBannerViewBrandBtnTag:
                    NSLog(@"跳转品牌商家");
                    break;
                case JHBannerViewDirectBtnTag:
                    NSLog(@"跳转厂家直销");
                    break;
                case JHBannerViewWholesaleBtnTag:
                    NSLog(@"跳转批发直送");
                    break;
                case JHBannerViewInventoryBtnTag:
                    NSLog(@"跳转库存尾货");
                    break;
                default:
                    break;
            }
        };
        
    }
    return _banner2;
}

你可能感兴趣的:(iOS 枚举+block的基础使用)