ios控件UIButton的封装

 通过block对button的点击事件封装

具体的实现过程

typedefvoid(^tapActionBlock)(UIButton *button);

@interfaceUIButton (Block)

@property(nonatomic,copy)tapActionBlock actionBlock;

//第一种封装

/**

 通过block对button的点击事件封装

 @param frame            frame

 @param title            按钮文字

 @param font              按钮文字的大小

 @param titleColor        按钮文字的颜色

 @param backgroundColor  按钮的背景颜色

 @param btnImageName      按钮的图片

 @param actionBlock      点击事件回调block

 @return                  button

 */

+ (UIButton *)createBtnFrame:(CGRect)frame title:(NSString *)title titleFont:(CGFloat)font titleColor:(UIColor *)titleColor  backgroundColor:(UIColor *)backgroundColor btnImageName:(NSString *)btnImageName action:(tapActionBlock)actionBlock;

在.m文件中实现其方法

staticNSString *keyOfUseCategoryMethod;//用分类方法创建的button,关联对象的key

staticNSString *keyOfBlock;

+(UIButton *)createBtnFrame:(CGRect)frame title:(NSString *)title titleFont:(CGFloat)font titleColor:(UIColor *)titleColor backgroundColor:(UIColor *)backgroundColor btnImageName:(NSString *)btnImageName action:(tapActionBlock)actionBlock{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    button.frame= frame;

    [button setTitle:title forState:UIControlStateNormal];

    button.titleLabel.font= [UIFont systemFontOfSize:font];

    [button setTitleColor:titleColor forState:UIControlStateNormal];

    button.backgroundColor= backgroundColor;

    [button setImage:[UIImage imageNamed:btnImageName]  forState:UIControlStateNormal];

    [button addTarget:button action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];


    /**

     *用runtime中的函数通过key关联对象

     *

     *objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

     *id object                    表示关联者,是一个对象,变量名理所当然也是object

     *const void *key              获取被关联者的索引key

     *id value                      被关联者,这里是一个block

     *objc_AssociationPolicy policy 关联时采用的协议,有assign,retain,copy等协议,一般使用OBJC_ASSOCIATION_RETAIN_NONATOMIC


     */

    objc_setAssociatedObject (button , &keyOfUseCategoryMethod, actionBlock,OBJC_ASSOCIATION_COPY_NONATOMIC);

    returnbutton;

}

按钮的点击事件

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

    /**

     * 通过key获取被关联对象

     *objc_getAssociatedObject(id object, const void *key)

     *

     */

    tapActionBlockblock = (tapActionBlock)objc_getAssociatedObject (sender , &keyOfUseCategoryMethod);


    if(block) {


        block(sender);


    }

}

- (void)setActionBlock:(tapActionBlock)actionBlock{

    objc_setAssociatedObject (self, &keyOfBlock, actionBlock,OBJC_ASSOCIATION_COPY_NONATOMIC);

}

- (tapActionBlock)actionBlock{

    returnobjc_getAssociatedObject (self, &keyOfBlock);

}



调用方法


UIButton *btn =[UIButtoncreateBtnFrame:CGRectMake(100,200,20,20)title:@"eeee"titleFont:12titleColor:[UIColor yellowColor]backgroundColor:[UIColor blueColor]btnImageName:@"headerDefault"action:^(UIButton *button) {

回调的按钮的点击事件

    }];

    [self.viewaddSubview:btn];

你可能感兴趣的:(ios控件UIButton的封装)