0

I have declared a UIImageView (IBOutlet UIImageView *character;) in a file called GameViewController.h and I am unsure how I can access this from another file CharacterSelectViewController.h.

All I need to do with it after I get it is to set the image. How can I access this UIImageView from another file?

Wain
  • 118,658
  • 15
  • 128
  • 151

1 Answers1

0

First you need to get the existing instance of GameViewController that is currently on display. How you do that from CharacterSelectViewController depends on how they are related to each other and you may want to set up a delegate relationship / navigate the view controller stack to find the correct instance.

Once you have the instance you just access the property:

gameViewController.character.image = ...;

In any case, a delegate type relationship is generally better than navigating the view controller hierarchy. This could be as simple direct relationship or a generalised relationship using an @protocol.

If GameViewController presented CharacterSelectViewController as a modal, then you could use

GameViewController *gameViewController = (GameViewController *)self.presentingViewController;

If GameViewController pushed CharacterSelectViewController into its navigation controller, then you could use:

NSArray *viewControllers = self.navigationController.viewControllers;
GameViewController *gameViewController = (GameViewController *) viewControllers[viewControllers.count - 2];
Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks for the quick answer Wain. How can I set up a delegate? I have not done this yet. – user3466512 Mar 26 '14 at 23:47
  • For a delegate: http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c – Wain Mar 26 '14 at 23:50
  • But you might just want to set a direct reference (i.e. add a property to `CharacterSelectViewController` and set it). Again, it depends how the controllers are related... – Wain Mar 26 '14 at 23:51