下面的實驗得出結論,本機文本屬性之前的類別屬性設置,所以該值可以按類別制定者可以安全地覆蓋。
的標籤類別:
// UILabel+Thingy.h
#import <UIKit/UIKit.h>
@interface UILabel (Thingy)
@property (nonatomic, strong) IBInspectable NSString *thingy;
@end
// UILabel+UILabel_Thingy.m
#import "UILabel+Thingy.h"
#import <objc/runtime.h>
@implementation UILabel (Thingy)
- (NSString *)thingy {
return objc_getAssociatedObject(self, @selector(thingy));
}
- (void)setThingy:(NSString *)thingy {
NSLog(@"setting thingy to '%@', my text is currently '%@'", thingy, self.text);
objc_setAssociatedObject(self, @selector(thingy), thingy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
在IB中,設置檢查的類別屬性和文本屬性....
在含視圖控制器小儀器:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"didLoad text is '%@' and thingy is '%@'", self.label.text, self.label.thingy);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"willAppear text is '%@' and thingy is '%@'", self.label.text, self.label.thingy);
}
運行它,並且NSLog輸出ind在從nib清醒期間,本機屬性由分類屬性設置器被調用時設置...
... [794:41622]將thingy設置爲'thingy value',我的文本是目前 '文本值'
... [794:41622] didload文字是 '文本值' 和啄爲 '啄值'
... [794:41622] willappear文字是 '文本值'而thingy是'thingy value'
在ca中設置標籤的文本屬性tegory屬性setter將(並且,我測試了它)導致text屬性被覆蓋到thingy屬性,因爲text屬性首先被初始化。
進一步的證據可以在XIB文件時作爲XML呈現可見...
<label opaque="NO" (... all the native properties) text="text value" (...) id="XAM-6h-4fn">
<rect key="frame" x="274" y="147" width="278" height="34"/>
(... and so on)
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="thingy" value="thingy value"/>
</userDefinedRuntimeAttributes>
</label>
...與視圖被實例化,並且經由預購遍歷初始化,從而其是一致在(子標籤)userDefinedRuntimeAttributes之前設置(父標籤)標籤屬性。
如果您以編程方式設置標籤的文本(如viewDidLoad中),或者您的意思是在IB中設置,您可以更清楚嗎? –
對不起,通過IB – bencallis