iOS开发-类别通过runtime增加原生控件属性

创建Category;

创建Objective-C file文件,下图File为类别名,Class为需要添加属性的原生类


类别创建


以UITextField为例;

.h文件

@interfaceUITextField (IndexPath)

@property (nonatomic,strong) NSIndexPath *indexPath;//增加indexPath属性

@end

.m文件

#import "UITextField+IndexPath.h"

#import

@implementationUITextField (IndexPath)

staticcharindexPathKey;

- (NSIndexPath*)indexPath{

    return objc_getAssociatedObject(self, &indexPathKey);

}

- (void)setIndexPath:(NSIndexPath*)indexPath{

    objc_setAssociatedObject(self, &indexPathKey, indexPath,OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

@end

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容