UILabel 没有收到我写入的内容

发布于 11-25 08:56 字数 544 浏览 2 评论 0原文

写这个主题很棘手。我想这是一个基本问题,但我似乎找不到答案。 代码本身显示了我想要执行的操作,而 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 确实输出了正确的内容。 这里发生了什么我没有看到的事情?

Tricky to write the subject to this. I guess this is a basic question but I can't seem to find the answer.
The code itself shows what I wanna do and the UILabel don't show anything, the first line I add to it works fine, but not when I try to write out the array:

-(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"];
}

}

The NSLog do put out the right stuff.
What is gong on here, that I am not seeing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

从﹋此江山别2024-12-02 08:56:47

在我看来,问题在于您每次都设置全文,而最后一个 setText: 带有 @"\n" 这是一个不可见的字符串。尝试附加而不是设置文本。类似于:

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

这会将@“TextToAppend”附加到标签中的当前文本值。

更新:请注意,我使用的是文本属性而不是设置器。

historyLabel.text = @"Some Text";

相当于

[historyLabel setText:@"Some Text"];

Seems to me that the problem is that you are setting the full text each time and the last setText: is with @"\n" which is an invisible string. Try appending instead of setting the text. Something like:

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

This will append @"TextToAppend" to the current text value in the label.

Update: Notice I'm using the text property rather than the setter.

historyLabel.text = @"Some Text";

is equivalent to

[historyLabel setText:@"Some Text"];
羁客2024-12-02 08:56:47

在字符串中使用 \n 应该没问题,尝试将标签的 numberOfLines 属性设置为 0,这允许其中包含任意数量的行。

Using \n in a string should be fine, try to set numberOfLines property of the label to 0, which allow any number of lines in it.

还给你自由2024-12-02 08:56:47

这是我自己的问题的解决方案。希望它可以帮助别人。

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;

This is the solution for my own problem. Hope that it can help someone else.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文