把这个话题写成了这个主题。我想这是一个基本问题,但我似乎无法找到答案。 代码本身显示了我想做的事情,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确实推出了正确的东西。 什么是锣,我没有看到?
答案 0 :(得分:1)
在我看来,问题在于您每次都要设置全文,而最后一个 setText:是@“\ n”,这是一个不可见的字符串。尝试添加而不是设置文本。类似的东西:
historyLabel.text = [NSString stringWithFormat:@"%@%@", historyLabel.text,@"TextToAppend"];
这会将@“TextToAppend”附加到标签中的当前文本值。
更新:注意我正在使用text属性而不是setter。
historyLabel.text = @"Some Text";
相当于
[historyLabel setText:@"Some Text"];
答案 1 :(得分:0)
在字符串中使用\n
应该没问题,尝试将标签的numberOfLines
属性设置为0,这样就可以在其中包含任意数量的行。
答案 2 :(得分: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;