2011-07-22 16 views
0

棘手的問題寫到這個主題。我想這是一個基本問題,但我似乎無法找到答案。 代碼本身就說明了我想做的事和的UILabel不顯示任何東西,第一行我添加到它工作正常,但不是當我嘗試寫出來的數組:UILabel沒有得到我寫給它的信息

-(IBAction)getSongHistory:(id)sender { 
[historyLabel setText:@"test write\n test write another line"]; 
NSArray *pastMusicArray = [pastSongs getHistory]; 
for(int t=2; t<[pastMusicArray count]; t++) { 
    NSString *tempRow = [pastMusicArray objectAtIndex:t]; 
    //NSLog(@"%@", tempRow); 
    [historyLabel setText:tempRow]; 
    [historyLabel setText:@"\n"]; 
} 

}

NSLog確實發佈了正確的東西。 這是什麼鑼,我沒有看到?

回答

1

對我來說,問題在於你每次都設置全文,而最後的setText:與@「\ n」是一個不可見的字符串。嘗試追加而不是設置文本。例如:

historyLabel.text = [NSString stringWithFormat:@"%@%@", historyLabel.text,@"TextToAppend"]; 

這會將@「TextToAppend」附加到標籤中的當前文本值。

更新:通知我正在使用文本屬性,而不是setter。

historyLabel.text = @"Some Text"; 

相當於

[historyLabel setText:@"Some Text"]; 
+0

問題是setText替換了UILabel中的所有內容,因此我的最後一個setText是@「\ n」。這意味着一個空白的UILabel。 –

+0

正是我所說的。 – fsaint

0

在字符串中使用\n要細,儘量標籤的numberOfLines屬性設置爲0,允許包含任意數目的線路。

+0

我在界面生成器中將numberOfLines設置爲0,但直到我設置了代碼才做到這一點。 –

0

這是我自己的問題的解決方案。希望它能幫助別人。

NSArray *pastMusicArray = [pastSongs getHistory]; 
musicHistory = [[NSMutableString alloc] init]; 
historyLabel.numberOfLines = 0; 
for(int t=2; t<[pastMusicArray count]; t++) { 
    [musicHistory appendString:[[pastMusicArray objectAtIndex:t] capitalizedString]]; 
    [musicHistory appendString:@"\n"]; 
} 
historyLabel.text = musicHistory;