类别添加属性,即runtime运行时添加属性(动态属性)
来源:互联网 发布:闲鱼网淘宝二手电脑版 编辑:程序博客网 时间:2024/04/28 20:23
在自定义扩展类时,可以通过运行时添加属性。
关键点在于,首先必须导入运行时头文件;
#import <objc/runtime.h>
其次,设置属性;
objc_setAssociatedObject(self, &keyLineBottom, lineBottom, OBJC_ASSOCIATION_RETAIN);
还有是获得设置的属性;
UIImageView *lineBottom = objc_getAssociatedObject(self, &keyLineBottom);
还有一点需要设置,即设置关联key值
static NSString *const keyLineBottom = @"lineBottomView";
注意:扩展类中添加的属性必须是对象类型,如要添加NSInteger类型,则需转换成NSNumber类型。
示例如下(UITableViewCell添加底端分割线属性):
.h文件
#import <UIKit/UIKit.h>@interface UITableViewCell (CellLine)///设置分割线- (void)showLineView:(CGFloat)offX color:(UIColor *)lineColor type:(CellLineShowType)type;///底端分割线@property (nonatomic, strong) UIImageView *lineViewButtom;@end
.m文件
#import "UITableViewCell+CellLine.h"#import <objc/runtime.h>static NSString *const keyLineTop = @"lineTopView";static NSString *const keyLineBottom = @"lineBottomView";static CGFloat const heightLine = 0.5; // 默认分割线高度@implementation UITableViewCell (CellLine)///设置分割线- (void)showLineView:(CGFloat)offX color:(UIColor *)lineColor type:(CellLineShowType)type{ // 底端分割线 UIImageView *lineBottom = objc_getAssociatedObject(self, &keyLineBottom); if (!lineBottom) { lineBottom = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, (CGRectGetHeight(self.bounds) - heightLine), CGRectGetWidth(self.bounds), heightLine)]; [self.contentView addSubview:lineBottom]; objc_setAssociatedObject(self, &keyLineBottom, lineBottom, OBJC_ASSOCIATION_RETAIN); } CGRect rectButtom = lineBottom.frame; rectButtom.origin.x = offX; lineBottom.frame = rectButtom; lineBottom.backgroundColor = lineColor; lineBottom.image = [UIImage imageWithColor:lineColor andSize:CGSizeMake(1.0, 1.0)]; // 顶端分割线 UIImageView *lineTop = objc_getAssociatedObject(self, &keyLineTop); if (!lineTop) { lineTop = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.bounds), heightLine)]; [self.contentView addSubview:lineTop]; objc_setAssociatedObject(self, &keyLineTop, lineTop, OBJC_ASSOCIATION_RETAIN); } CGRect rectTop = lineTop.frame; rectTop.origin.x = offX; lineTop.frame = rectTop; lineTop.backgroundColor = lineColor; lineTop.image = [UIImage imageWithColor:lineColor andSize:CGSizeMake(1.0, 1.0)]; switch (type) { case CellLineShowNone: { lineBottom.hidden = YES; lineTop.hidden = YES; } break; case CellLineShowAll: { lineBottom.hidden = NO; lineTop.hidden = NO; } break; case CellLineShowButtom: { lineBottom.hidden = NO; lineTop.hidden = YES; } break; case CellLineShowTop: { lineBottom.hidden = YES; lineTop.hidden = NO; } break; default: break; }}#pragma mark - setter/getter- (void)setLineViewButtom:(UIImageView *)lineViewButtom{ objc_setAssociatedObject(self, &keyLineBottom, lineViewButtom, OBJC_ASSOCIATION_RETAIN);}- (UIImageView *)lineViewButtom{ UIImageView *line = objc_getAssociatedObject(self, &keyLineBottom); return line;}- (void)setLineViewTop:(UIImageView *)lineViewTop{ objc_setAssociatedObject(self, &keyLineTop, lineViewTop, OBJC_ASSOCIATION_RETAIN);}- (UIImageView *)lineViewTop{ UIImageView *line = objc_getAssociatedObject(self, &keyLineTop); return line;}@end
0 0
- 类别添加属性,即runtime运行时添加属性(动态属性)
- Runtime(三)利用运行时来为类别添加属性
- 动态添加属性(Runtime)
- runtime动态添加属性
- Runtime 动态添加属性
- runtime -- 动态添加属性
- 利用runtime给类别添加属性
- iOS Runtime应用实例(一)类别添加属性
- iOS Runtime应用实例(一)类别添加属性
- iOS (Category)runtime动态添加属性
- Runtime(动态添加属性)学习
- iOS在分类中添加属性,运行时编程,objc runtime 动态增加属性
- 类别添加属性
- 类别添加属性
- 利用runtime添加属性
- iOS为类别添加属性的方法(RunTime)
- runtime跳转界面以及动态添加属性
- runtime简单使用之动态添加属性
- UIResponder - 3
- linux嵌入式开发+驱动开发
- (二)Method Swizzling 和 AOP 实践,(方法交换)
- MySQL学习笔记-Keepalived VIP配置使用
- JNI ERROR (app bug): attempt to use stale local reference 0x31900019(Android ICS(4.0+) JNI局部应用的变化)
- 类别添加属性,即runtime运行时添加属性(动态属性)
- 关于Unity启动时间过长(启动黑屏时间长)的问题
- iOS-设置启动页面时间
- 传智博客2015年~C语言深入浅出
- javaweb:在线聊天网站
- iOS下微信语音播放之切换听筒和扬声器的方法解决方案
- 关于有偿提供拼图响应式后台的通知
- hibernate 一对多级联
- 黑马安卓46期android开发