这可能不是一个好的工作方式。你想要的是一个 imageViews 数组。然后你只需要一个数字索引,你可以通过 imageViews 数组隐藏所有没有选择索引的东西。
但是你如何得到一个 imageViews 数组呢?请参阅如何从对象数组中制作 IBOutlets? 它解释了如何使用 IBOutletCollection。
如果每个视图都有一个单独的按钮,请将它们也放入 IBOutletCollection 中。这样你就可以拥有这样的东西:
- (IBAction) imageButtonPressed:(id) sender;
{
// The sender is the button that was just pressed.
NSUInteger chosenIndex = [[self imageButtons] objectAtIndex:sender];
for (NSUInteger imageIndex = 0; imageIndex < [[self imageViews] count]; imageIndex++)
{
// Hide all views other than the one associated with the pressed button.
if (imageIndex != chosenIndex)
{
[[[self imageViews] objectAtIndex:imageIndex] setHidden:YES];
}
else
{
[[[self imageViews] objectAtIndex:imageIndex] setHidden:NO];
}
}
}
如果你真的,真的需要将字符串image1
与 imageView 相关联,你可以构造一个NSDictionary
将你的控件与唯一的字符串标识符相关联,以便以后查找。NSDictionary 很强大,但我对为什么需要它的原因持空白。
NSMutableDictionary *viewLookup;
[viewLookup setObject:[[self imageViews] objectAtIndex:0] forKey:@"image0"];
[viewLookup setObject:[[self imageViews] objectAtIndex:1] forKey:@"image1"];
[viewLookup setObject:[[self imageViews] objectAtIndex:2] forKey:@"image2"];
[viewLookup setObject:[[self imageButtons] objectAtIndex:0] forKey:@"button0"];
// ...
// Can now look up views by name.
// ...
NSString *viewName = @"image1";
UIView *viewFound = [viewLookup objectForKey:viewName];
[viewFound doSomething];