【问题标题】:Displaying array contents显示数组内容
【发布时间】:2012-02-06 18:09:41
【问题描述】:

我已经加载了一个数组:

currentBackground=4;
bgImages = [[NSArray alloc] initWithObjects:
            [UIImage imageNamed:@"mystery_01 320x460"],
            [UIImage imageNamed:@"mystery_02 320x460"],
            [UIImage imageNamed:@"mystery_03 320x460"],
            [UIImage imageNamed:@"mystery_04 320x460"],
            [UIImage imageNamed:@"mystery_05 320x460"],
            nil];

现在我想在标签中显示当前正在显示的图像的文件名。我想:

 mainLabel.text = [NSString stringWithFormat:@"bgImages= %@",[bgImages objectAtIndex:currentBackground]];

可以,但我得到的只是十六进制代码。我有一个按钮可以很好地滚动图像。但是当我尝试显示图像名称时,我得到的只是我认为是名称所在的地址。

想法。

【问题讨论】:

  • 您使用什么技术?适当地重新标记将有助于向正确的受众展示问题,并获得更好的答案。

标签: objective-c ios arrays text


【解决方案1】:

当您这样做时 - [bgImages objectAtIndex:currentBackground] - 您将获得一个 UIImage 的实例。当您执行stringWithFormat 时,您是对的,它正在打印图像的地址,仅此而已。

不幸的是,无法从 UIImage 实例中获取图像的“名称”,因此您可能必须执行以下操作:

currentBackground=4;
bgImages = [[NSArray alloc] initWithObjects:
            [UIImage imageNamed:@"mystery_01 320x460"],
            [UIImage imageNamed:@"mystery_02 320x460"],
            [UIImage imageNamed:@"mystery_03 320x460"],
            [UIImage imageNamed:@"mystery_04 320x460"],
            [UIImage imageNamed:@"mystery_05 320x460"],
            nil];
bgImageNames = [[NSArray alloc] initWithObjects:
                @"mystery_01 320x460",
                @"mystery_02 320x460",
                @"mystery_03 320x460",
                @"mystery_04 320x460",
                @"mystery_05 320x460",
                nil];

然后做:

mainLabel.text = [NSString stringWithFormat:@"bgImages= %@",[bgImageNames objectAtIndex:currentBackground]];

或者将 bgImages 数组的创建稍微包装一下,您甚至可以聪明地这样做:

currentBackground=4;
bgImageNames = [[NSArray alloc] initWithObjects:
                @"mystery_01 320x460",
                @"mystery_02 320x460",
                @"mystery_03 320x460",
                @"mystery_04 320x460",
                @"mystery_05 320x460",
                nil];
NSMutableArray *newBgImages = [NSMutableArray arrayWithCapacity:0];
for (NSString *image in bgImageNames) {
    [newBgImages addObject:[UIImage imageNamed:image]];
}
bgImages = [NSArray arrayWithArray:newBgImages];

【讨论】:

  • 感谢马特,这很有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多