zoukankan      html  css  js  c++  java
  • 关联:objc_getAssociatedObject和objc_setAssociatedObject使用

    为UIButton的category添加属性

    UIButton+subTitle.h

    #import <UIKit/UIKit.h>
    
    #import <objc/runtime.h>
    
     
    
    @interface UIButton (subTitle)
    
    @property (nonatomic, copy) NSString *subTitle;
    
    @property (nonatomic, strong) UIColor *foreColor;
    
    //此处添加int类型属性不能使用,因为objc需要用id类型,此处留着以后修改
    
    @property (nonatomic) int tagAdd;
    
    @end

    UIButton+subTitle.m

    #import "UIButton+subTitle.h"
    
     
    
    @implementation UIButton (subTitle)
    
     
    
    static char oooo;
    
     
    
    - (NSString *)subTitle{
    
     
    
        return objc_getAssociatedObject(self, @selector(subTitle));
    
    }
    
    - (void)setSubTitle:(NSString *)subTitle{
    
        [self setTitle:subTitle forState:UIControlStateNormal];
    
        objc_setAssociatedObject(self, @selector(subTitle), subTitle, OBJC_ASSOCIATION_COPY_NONATOMIC);
    
    }
    
    - (UIColor *)foreColor{
    
        return objc_getAssociatedObject(self, @selector(foreColor));
    
    }
    
    - (void)setForeColor:(UIColor *)foreColor{
    
        self.backgroundColor = foreColor;
    
        objc_setAssociatedObject(self, @selector(foreColor), foreColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    }
    
     
    
    - (void)setTagAdd:(int)tagAdd{
    
        objc_setAssociatedObject(self, &oooo, @(tagAdd), OBJC_ASSOCIATION_ASSIGN);
    
    }
    
    - (int)tagAdd{
    
        return (int)objc_getAssociatedObject(self, &oooo);
    
    }
  • 相关阅读:
    提升ASP.NET性能
    人性的弱点
    墨菲定律
    沟通
    网站
    程序员思维模式
    CSS
    HTML
    路由和数据传递(04)
    Sql Server中查看所有数据库,表名,字段名以及字段类型
  • 原文地址:https://www.cnblogs.com/On1Key/p/5144896.html
Copyright © 2011-2022 走看看