2014-09-28 38 views
-1

在我的應用程序中,我創建了一個'NSMutalbeArray',其中添加了一些'UILabel'。UILabel在NSMutableArray中無法識別的選擇器

我正確初始化我的數組:

_pickerMonthLabel = [[NSMutableArray alloc] init]; 

我正確添加UILabels在我的陣列,但是當我想要做一些更改標籤,該行爲很奇怪。例如,我想更改數組中標籤的文本顏色。我做的:

[(UILabel *)[_pickerMonthLabel objectAtIndex:i] setTextColor:[UIColor redColor]]; 

這看起來並不像,因爲我的應用程序崩潰的工作:

-[__NSCFConstantString setTextColor:]: unrecognized selector sent to instance 0x17e8d4 
*** Terminating app due to uncaught exception  
'NSInvalidArgumentException', reason: '-[__NSCFConstantString setTextColor:]: unrecognized selector sent to instance 0x17e8d4' 

,但我不報警,當我寫了這條線。當我寫了另一種線:

(UILabel *)[_pickerMonthLabel objectAtIndex:i].textcolor = [UIColor redColor]; 

它給了我一個錯誤:

Property 'textcolor' not found on object of type 'id' 

我不明白,這兩條線之間的區別,以及如何解決我的問題...

非常感謝您的幫助。

+0

該錯誤表明您的數組包含'NSString'對象,而不是'UILabel'對象。你確定你將標籤添加到數組而不是標籤的文本? – rmaddy 2014-09-28 17:24:38

+0

謝謝Maddy你的回答。在起點,我添加了NSString,但是我用UILabels替換了所有這些,這真的很奇怪。我用「在索引插入對象」來替換nsstring我的標籤 – user1833903 2014-09-28 17:30:27

+0

錯誤表明你沒有用標籤替換所有的字符串。 – rmaddy 2014-09-28 17:32:02

回答

0

我想這更多的是一句話,但你可以嘗試使用respondsToSelector:消息來檢查實例是否可以處理你試圖發送的消息。 (這將確保你沒有得到例外)在你的情況下,這將是:

if ([[_pickerMonthLabel objectAtIndex:i] respondsToSelector:@selector(setTextColor:)]){ 
    [(UILabel *)[_pickerMonthLabel objectAtIndex:i] setTextColor:[UIColor redColor]]; 
} 
// add this to see what your class is 
//else{ 
// NSLog(@"The class is: %@", [[_pickerMonthLabel objectAtIndex:i] class]); 
//} 

希望它有助於您的調試。

+0

謝謝你的回答 – user1833903 2014-09-28 20:59:18

相關問題