setting new properties in category interface/implementation

后端 未结 7 1510
醉梦人生
醉梦人生 2020-12-03 03:42

Ok, so I have this, but it wont work:

@interface UILabel (touches)

@property (nonatomic) BOOL isMethodStep;

@end


@implementation UILabel (touches)

-(BOO         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 04:04

    There is actually a way, which may not be ideal, but does work.
    For it to work, you will need to create a category for a class X and can only be used on subclasses of the same X (e.g. category UIView (Background) can be used with class MyView : UIView, but not directly with UIView)

    // UIView+Background.h
    
    @interface UIView (Background)
    
    @property (strong, nonatomic) NSString *hexColor;
    
    - (void)someMethodThatUsesHexColor;
    
    @end
    
    // UIView+Background.h
    
    @implementation UIView (Background)
    
    @dynamic hexColor; // Must be declared as dynamic
    
    - (void)someMethodThatUsesHexColor {
        NSLog(@"Color %@", self.hexColor);
    }
    
    @end
    

    Then

    // MyView.m
    
    #import "UIView+Background.h"
    
    @interface MyView : UIView
    
    @property (strong, nonatomic) NSString *hexColor;
    
    @end
    
    @implementation MyView ()
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setHexColor:@"#BABACA"];
        [self someMethodThatUsesHexColor];
    }
    
    @end
    

    Using this method, you will need to "redeclare" your properties, but after that, you can do all of its manipulation inside your category.

提交回复
热议问题