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);
    
    }
  • 相关阅读:
    在Windows下生成SSH文件
    git常用命令总结
    小Q的歌单
    在vmware下安装Ubuntu16-04
    hexo-next博客中mathjax显示问题解决
    可乐复制问题
    hexo-next博客添加评论功能
    hexo-next博客添加在线联系功能
    tableau desktop
    tableau desktop
  • 原文地址:https://www.cnblogs.com/On1Key/p/5144896.html
Copyright © 2011-2022 走看看