0

So basically I have UIButtons with different images as the buttons and a UIImageView in a different view controller. I want that when I click on one of the buttons, the image of the button will show up in the UIImageview in a different view controller.

Think of it as selecting a character in a game so when you select that character you play as that character.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

1

So give the second VC (view controller) an image property. When you click the button, invoke a segue, and then in prepareForSegue, set the image property in the second VC.

In the second VC's viewWillAppear, take the image property and install it in the image view.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • thank you but could you elaborate on how to invoke a segue and the prepareForSegue – user3667158 May 23 '14 at 00:46
  • Put some effort into it. Go do some searching on prepareFromSegue. Try it. If you have trouble, post the code that you're struggling with. – Duncan C May 23 '14 at 01:02
0

Something like this:

if ([[segue identifier] isEqualToString:@"toDifferentViewController"]) 
{
    UIImage *testImg;
    testImg = [UIImage imageNamed:@"Test.png"];
    differentViewController *destVC = segue.destinationViewController;
    destVC.passedImage = testImg;
}

In differentViewController.h set the property:

@property UIImage *passedImage;

in .m synthesize and use your UIImage as needed

@Synthesize passedImage; 
BossBols
  • 175
  • 12
  • do i need to have two different .h .m for each of the view controllers @BossBols – user3667158 May 23 '14 at 01:10
  • Every time you create a class file that inherits from UIViewController for example Xcode will automatically create the relevant .h and .m, which are needed yes! – BossBols May 23 '14 at 01:24
  • No, you don't need to put all class interfaces and implementation in different files — but you want it! – vikingosegundo May 23 '14 at 01:30