0
我在我的viewController中設置了3 UITextField
。如果不先調用setValueForKey,我會得到什麼值?
@property(nonatomic,retain) UITextField *line1;
@property(nonatomic,retain) UITextField *line2;
@property(nonatomic,retain) UITextField *line3;
我嘗試獲取價值的關鍵:
for (int i = 1; i <= 3; i++)
{
NSString *fieldName = [NSString stringWithFormat:@"line%d",i ];
UITextField *theField = [self valueForKey:fieldName];
NSLog(@"TEXTFIELD : %@",theField);
}
而且我得到了所有UITextFields。
2015-03-03 10:32:32.776 KVCTEST[26691:1586602] TEXTFIELD : <UITextField: 0x7f9a00e1c5c0; frame = (50 120; 220 30); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7f9a00e1ae40>>
2015-03-03 10:32:32.777 KVCTEST[26691:1586602] TEXTFIELD : <UITextField: 0x7f9a00f1b920; frame = (50 160; 220 30); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7f9a00f1bb80>>
2015-03-03 10:32:32.777 KVCTEST[26691:1586602] TEXTFIELD : <UITextField: 0x7f9a00f1d330; frame = (50 200; 220 30); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7f9a00f1d590>>
但是,如果我將實例變量名更改爲例如LINE1
,LINE2
,LINE3
,異常會提高。
那麼key-value-coding會默認與ivar的名字相關聯嗎?首先我不需要setValueForKey
?
你的答案是下valueForKey'默認搜索模式:'在[文件](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/SearchImplementation.html #// apple_ref/DOC/UID/20000955) – MendyK 2015-03-03 03:18:46