iOS-为系统控件添加属性
2019-05-29 本文已影响0人
拎着猫走的鱼
例如为UIImage添加一个照片No
-
首先创建UIImage的分类,命名为PhotoNo
-
UIImage+PhotoNo.h
#import <UIKit/UIKit.h>
@interface UIImage (PhotoNo)
@property(nonatomic,copy) NSString *photoNo;
@end
- UIImage+PhotoNo.m
#import "UIImage+PhotoNo.h"
#import <objc/runtime.h>
static NSString *photoNoKey = @"photoNoKey";
@implementation UIImage (PhotoNo)
- (void)setPhotoNo:(NSString *)photoNo {
objc_setAssociatedObject(self, &photoNoKey, photoNo, OBJC_ASSOCIATION_COPY);
}
- (NSString *)photoNo {
return objc_getAssociatedObject(self, &photoNoKey);
}
@end
- 使用实例
UIImage *image = [UIImage imageNamed:@"test.png"];
image.photoNo = @"test123";