Objective-C: Property / instance variable in category

前端 未结 6 1416
清酒与你
清酒与你 2020-11-22 16:56

As I cannot create a synthesized property in a Category in Objective-C, I do not know how to optimize the following code:

@interface MyClass (Variant)
@prope         


        
6条回答
  •  臣服心动
    2020-11-22 17:52

    Tested only with iOS 9 Example: Adding an UIView property to UINavigationBar (Category)

    UINavigationBar+Helper.h

    #import 
    
    @interface UINavigationBar (Helper)
    @property (nonatomic, strong) UIView *tkLogoView;
    @end
    

    UINavigationBar+Helper.m

    #import "UINavigationBar+Helper.h"
    #import 
    
    #define kTKLogoViewKey @"tkLogoView"
    
    @implementation UINavigationBar (Helper)
    
    - (void)setTkLogoView:(UIView *)tkLogoView {
        objc_setAssociatedObject(self, kTKLogoViewKey, tkLogoView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (UIView *)tkLogoView {
        return objc_getAssociatedObject(self, kTKLogoViewKey);
    }
    
    @end
    

提交回复
热议问题