我讀了Customizing Existing Classes和Objective-C Runtime Reference的蘋果開發者文檔。使用objc_setAssociated和objc_getAssociated的條件
我想知道objc_getAssociatedObject
和objc_setAssociatedObject
方法是否必須與CATEGORY一起使用。
是否意味着類別用於定製方法和objc_getAssociatedObject
和objc_setAssociatedObject
是用於定製(通常添加)伊娃?
是感覺使用上面的方法分別添加另一個實例變量嗎?
如果是,那麼添加實例變量的條件是什麼?
謝謝。
最新編輯:
ViewController.m
[XXView showView:[UIColor greenColor]];
XXView.m
+ (void)showView: (UIColor *)bgcolor {
XXViewController *vc = [[XXViewController alloc] init];
vc.backgroundColor = [self BackgroundColor];
}
+ (void)setBackgroundColor:(UIColor *)BackgroundColor {
objc_setAssociatedObject(self, @selector(backgroundColor), BackgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (UIColor *)BackgroundColor {
if (!objc_getAssociatedObject(self, _cmd)) {
[self setBackgroundColor:[UIColor redColor]];
}
return objc_getAssociatedObject(self, _cmd);
}