我遇到了数组和在视图之间传递图像的问题,我需要一些帮助!所以背景是我有:
• RootViewController(处理我的表格视图)
• CommunicationViewController,它处理表格中选定元素的详细信息
• SelectSlideViewController 显示从 CommunicationViewController 中单击的图像,并允许用户从相机胶卷中选择不同的图像
所以问题:
• 在 CommunicationViewConroller 中,如果用户单击一个按钮,我有以下代码:
- (IBAction) selectSlide:(id) sender
{
if(self.selectSlideView == nil)
{
SelectSlideViewController *viewController = [[SelectSlideViewController alloc]
initWithNibName:@"SelectSlideViewController" bundle:[NSBundle mainBundle]];
self.selectSlideView = viewController;
[viewController release];
}
[self.navigationController pushViewController:self.selectSlideView animated:YES];
self.selectSlideView.cmn = cmn;
self.selectSlideView.title = cmn.name;
self.selectSlideView.imageView.image = self.myImage5;
}
上面的代码有效,例如,如果我单击按钮 5,因为它将 image5 设置为“SelectSlideViewController”中的视图。
但是,我希望多个按钮使用相同的“selectSlide”动作——为此,我需要确定按下了哪个按钮,然后从图像数组(或一系列笨拙的 if-else 语句)。
• 所以我修改后的代码如下,但它不适用于数组——有什么想法吗?:
- (IBAction) selectSlide:(id) sender
{
if(self.selectSlideView == nil)
{
SelectSlideViewController *viewController = [[SelectSlideViewController alloc]
initWithNibName:@"SelectSlideViewController" bundle:[NSBundle mainBundle]];
self.selectSlideView = viewController;
[viewController release];
}
NSUInteger tmpInt = -1;
tmpInt = [buttonArray indexOfObject:sender];
[self.navigationController pushViewController:self.selectSlideView animated:YES];
self.selectSlideView.cmn = cmn;
self.selectSlideView.title = cmn.name;
NSLog(@"The int was %d",tmpInt);
NSLog(@"This is the image array size %d ",[imageArray count]);
If(tmpInt >-1 && tmpInt <9)
{
self.selectSlideView.imageView.image = [imageArray objectAtIndex:tmpInt];
}
/** this code works, but is a bit clunky:
if(tmpInt == 0)
self.selectSlideView.imageView.image = self.myImage1;
else if (tmpInt == 1)
self.selectSlideView.imageView.image = self.myImage2;
else if (tmpInt == 2)
self.selectSlideView.imageView.image = self.myImage3;
else if (tmpInt == 3)
self.selectSlideView.imageView.image = self.myImage4;
else if (tmpInt == 4)
self.selectSlideView.imageView.image = self.myImage5;
else if (tmpInt == 5)
self.selectSlideView.imageView.image = self.myImage6;
else if (tmpInt == 6)
self.selectSlideView.imageView.image = self.myImage7;
else if (tmpInt == 7)
self.selectSlideView.imageView.image = self.myImage8;
else if (tmpInt == 8)
self.selectSlideView.imageView.image = self.myImage9;
**/
}
我已经明确确认 imageArray 的大小为 9,并且它正在从数组中选择正确的索引,但是如果从数组中访问,则 SelectSlideViewController 不会显示图像。如果使用笨拙的 if-else 语句访问,它会显示它。