我正在寻找传递依赖于按钮选择的图像。 我的FirstController上有Button1,Button2,Button3。
我想当我按下Button1时,在SecondController上将image.png显示的图像显示在Imageview中已插入到我的视图中。 同样如果我按下button2,图像名为photo2.png显示在同一Imageview上。 我真的不知道该怎么做我想要的。 我想将图片插入此代码:
- (IBAction)Button1:(id)sender {
SecondController *dvController = [[SecondController alloc]
initWithNibName:@"SecondController" bundle:[NSBundle mainBundle]];
dvController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
答案 0 :(得分:0)
SecondController
应该有一个property
,例如UIImage* previousViewImage
,您可以通过dvController.previousViewImage= [(UIButton*)sender imageForState:UIControlStateNormal].
答案 1 :(得分:0)
一种方法是拥有一组图像名称,然后使用按钮的标记作为索引从数组中获取图像名称。
首先让一个按钮和一个IBAction链接到它。然后复制并粘贴按钮。这为您提供了许多按钮,但只有一段代码。为每个按钮设置不同的标记属性,代码使用[sender tag]作为数组的索引。
然后dvController有一个UIImage属性,在呈现之前从该数组设置。在dvController的viewWillAppear中,然后将UIImageView设置为传递的UIImage。
答案 2 :(得分:0)
- (IBAction)Button1:(id)sender {
SecondController *dvController = [[SecondController alloc]
initWithNibName:@“SecondController”包:[NSBundle mainBundle]]; dvController.imageName = @“photo”,// imageName是您将在第二个视图控制器中声明的字符串 dvController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:dvController animated:YES]; [dvController发布]; dvController = nil; }
并且在第二个视图控制器的视图中加载了你可以做到这一点
imageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];// imageview is the name of your imageview outlet .
类似于第二个按钮,您可以设置dvController.imageName=@"photo2"// put the name without png
。希望这有帮助。