按下按钮时在视图之间传递图像

时间:2012-09-23 13:46:28

标签: iphone xcode image sdk

我正在寻找传递依赖于按钮选择的图像。 我的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;
}

3 个答案:

答案 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)

你可以做的就是在你的SecondController.h中声明并合成一个字符串,所以当按下第一个按钮时你可以设置字符串的值,并在你想要设置的图像的名称中设置第二个视图控制器在您的方法中的图像视图

 - (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。希望这有帮助。