ios - 如何将 NSMutable 字符串附加到 UILabel

标签 ios objective-c

这是我向 Stack Overflow 提出的第一个问题。我已经使用这个网站一段时间了,并使用它的资源来找出我的编程问题的答案,但我恐怕这次找不到我正在寻找的答案。

我创建了这五个字符串:

//List five items from the book and turn them into strings

//1 Josh the Trucker
NSString *stringJosh = @"Josh the Trucker";

//2 The Witch from the Remote Town
NSString *stringWitch = @"The Witch from the Remote Town";

//3 Accepting the curse rules "Willingly and Knowingly"
NSString *stringRules = @"Accepting the curse rules, --Willingly and Knowingly--";

//4 Josh's time left to live--Five Days Alive Permitted
NSString *stringFiveDays = @"Josh's time left to live--Five Days Alive Permitted";

//5 The Fire Demon Elelmental    
NSString *stringDemon = @"The Fire Demon Elelmental";

然后,我将它们放在一个数组中:

//Create an array of five items from the book
      NSArray *itemsArray = [[NSArray alloc] initWithObjects:
                            stringJosh,
                            stringWitch,
                            stringRules,
                            stringFiveDays,
                            stringDemon,
                            nil];

然后,我创建了这个可变字符串,我需要在其中循环遍历数组并将项目附加到 UIlabel。

 NSMutableString *itemsString = [[NSMutableString alloc] initWithString:
                                  @"itemsArray"];

这是循环,它显示控制台日志中的项目。

for (int i=0; i<5; i++)
{
    NSLog(@"Book Item %d=%@", i, itemsArray[i]);

}

我的问题是,如何将这些项目附加到 UIlabel 中? 这些功能在我的 appledelegate 中。

在我的 viewDidAppear 函数 (flipsideViewController) 中,我有:

label8.text =""----thats where the looped info needs to go.

我该怎么做? 我觉得我需要将它们放在一起并附加到 NSLog 应该在的位置......但是我如何将该信息传输到文本标签?

我希望我自己解释清楚了。

我们还没有做任何附加示例,我想这是我需要从“野外”那里得到答案的地方

这是我所知道的最疯狂的编码环境,所以我希望我能在这里找到一些方向。

感谢您的关注!

最佳答案

一旦你有了所有你想在 NSArray 中连接的字符串,你就可以将它们与单个调用结合起来(使用你想要的任何分隔符):

NSString *combinedString = [itemsArray componentsJoinedByString:@" "];

如果您需要更复杂的逻辑,您可以使用 NSMutableString 在迭代数组时创建您想要的结果,即:

NSMutableString *combinedString = [NSMutableString string];
[itemsArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
       [combinedString appendFormat:@"Book Item %d=%@ ", idx, obj];
    }];

另请注意,最好使用快速枚举或 block 枚举遍历集合,而不是使用普通的基于索引的 for 循环。

关于ios - 如何将 NSMutable 字符串附加到 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18228975/

相关文章:

ios - 使用推送通知配置文件安装应用程序失败

iOS调用transitionWithView时出现不必要的延迟:

ios - UI 搜索栏。无法更改框架

iOS绘图仿平尖笔

objective-c - 如何在 IOS 中心水平对齐我的图像

ios - 使用 Carthage 导入 CocoaLumberjack

ios - 管理 View Controller 导航的最佳方式

ios - 如何将 UIPageControl 连接到 UICollectionView (Swift)

objective-c - objective-c 中的嵌套方法调用

objective-c - 为什么信号不会被捕获?