2013-02-07 176 views
0

看看下面的代碼:如何使用點符號內的字符串訪問屬性?

@property (weak, nonatomic) UILabel *l112; 
@property (weak, nonatomic) UILabel *l212: 
@property (weak, nonatomic) UILabel *l312: 

((RBScorecardVC *)self.presentingViewController).l112.text = @"Hello" 
((RBScorecardVC *)self.presentingViewController).l212.text = @"Hello" 
((RBScorecardVC *)self.presentingViewController).l312.text = @"Hello" 

請注意,我怎麼設置我的標籤的所有文本爲「Hello」。必須有一個更有效的方式來做到這一點。我希望能夠以某種方式訪問​​UIlabels(l112,1212等)的名稱。這有意義嗎?在我的實際應用程序中,我並沒有將所有內容都設置爲「你好」,而是文本是計算的結果。此外,在我的實際應用程序,我有3組以上的標籤設置(有50+)下面是一個使用重複的if語句來sparately訪問每一個的UILabel我的實際代碼:

-(IBAction) submitScore: (id)sender 
{ 
self.stringID = ((RBScorecardVC *)self.presentingViewController).self.giveString; 

if (self.stringID isEqualToString:@"l112") 
{ ((RBScorecardVC *)self.presentingViewController).l112.text = [[NSString alloc]initWithFormat:@"%d", self.accumulator];} //l112 is the name of my UILabel 
else if (self.stringID is EqualToString:@"l212") 
{ ((RBScorecardVC *)self.presentingViewController).l212.text = [[NSString alloc]initWithFormat:@"%d", self.accumulator];} //l212 is the name of my UILabel 
} 

我希望能夠更有效地做到這一點是這樣的:

-(IBAction) submitScore: (id)sender 
self.stringID = ((RBScorecardVC *)self.presentingViewController).self.giveString; 

((RBScorecardVC *)self.presentingViewController).[UILabel withTitle:@"%@",stringID].text = [[NSString alloc]initWithFormat:@"%d", self.accumulator];} 

通知我[的UILabel withTitle:@ 「%@」 的StringID]點符號中使用的部分。我知道這不起作用,但我想知道如何正確地寫這個,所以stringID可以用來訪問我需要的UILabel的名稱?

+1

什麼是'+ [UILabel withTitle:]'? – 2013-02-07 23:01:07

+0

你應該使用一個NSArray,並有一個使用該數組的搜索方法。用戶界面元素NSArray可以通過'IBOutletCollection'設置。 – Vinzzz

回答

0

其他答案在這裏處理對象的屬性。在這種情況下,你試圖訪問對象本身。當你開始問自己一個這樣的問題時,你應該把所有的對象(在這種情況下是標籤)存儲在數組屬性中,而不是每個標籤的單獨屬性。而是想着/像l112引用標籤的命名變量,認爲他們是在陣列中的索引:

的UILabel * theLabelINeed = [self.myArray objectAtIndex:2]。 [theLabelINeed callWhateverMethodINeedTo];

或者更短,如果你從一個Python/Ruby的背景來了,這是給你更具可讀性:

[自我。myArray [2] callWhateverMethodINeedTo];

如果你需要快速訪問每一個標籤數組中,你可以在一個for循環使用快速列舉:

for (UILabel *currLabel in self.myArray) { 
    [currLabel callWhateverMethodINeedTo]; 
} 

你也可以使用一個NSDictionary相反,它可以讓你指定「鍵「或名稱的對象。

NSDictionary *dict {"l112": label1, "l113": label2}; // or some other dictionary creation method, maybe a mutable dictionary or whatever. 
UILabel *myLabel = [dict objectForKey:@"l112"]; 
+0

我喜歡數組的想法..我可以這樣做: [myLabelArray objectAtIndex:i] .text = @「無論我需要我的文本是什麼」; 如果數組中的所有對象都是UILabels,並且UILabels具有文本屬性,我可以如圖所示訪問該屬性嗎? – iOSAppGuy

+0

當然!在你的腦海中,你可以評估所有的括號,並回到他們的回報。因此'objectAtIndex:'返回一個'UILabel',然後在返回的'UILabel'實例上調用'setText:'。如果你最終使用我的答案,不要忘記標記爲正確的:) –

+1

我會試試看,看看這個值得的複選標記! NSDictionary選項看起來也很性感。謝謝! – iOSAppGuy

5

嘗試在您的UILabel上使用valueForKey:

然而,整潔的解決方案可能是將這些屬性簡單地存儲在NSDictionary中。

0

爲什麼你想使用點符號 - 最初是爲了object @properties - 用於別的東西?

Dot Notation vs Method Notation

你似乎通過Objective-C的語法困惑:爲什麼[UILabel withTitle:"%@", stringId],而不是簡單的[UILabel withTitle:stringID],或者如果是的StringID不是字符串,[UILabel withTitle:[NSString stringWithFormat:@"%@", stringID]]

另外,爲什麼是withTitleUILabel一個類的方法而不是你的presentingViewController的實例方法?

+0

(((RBScorecardVC *)self.presentingViewController) – iOSAppGuy

+0

是的,是嗎? 'RBSscorecardVC'很可能有一個' - (UILabel *)labelWithTitle:'實例方法,不是嗎? – Vinzzz

0

這樣的東西可能會做你在問什麼,但我同意使用NSDictionary的答案可能更易於維護。或者,您可以在IB中的標籤上使用整數標籤屬性,並調用父視圖的viewWithTag:方法。

- (UILabel *)labelWithName:(NSString *)name 
{ 
    SEL selector = NSSelectorFromString(name); 
    if (![self respondsToSelector:selector]) 
     return nil; 

#pragma clang diagnostic push 
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" 
    id result = [self performSelector:selector]; 
#pragma clang diagnostic pop 
    if (!result) 
     return nil; 
    if (![result isKindOfClass:[UILabel class]]) 
     return nil; 
    return (UILabel *)result; 
} 

- (IBAction)derp:(id)sender 
{ 
    UILabel *label = [self labelWithName:@"label1"]; 
    [label setText:@"I'm label 1"]; 
    label = [self labelWithName:@"label2"]; 
    [label setText:@"I'm label 2"]; 
} 
相關問題