0

I have created a storyboard that contains a view controller with a view. I'm writing separate code files for the view and the view controller, so I need a way to get a reference to the view in the storyboard file. Any suggestions?

2 Answers2

2

Give your view controller in IB an identifier then get it like this

MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainController"];

More details here

Community
  • 1
  • 1
Sam Jarman
  • 7,025
  • 14
  • 49
  • 98
1

There are several ways to just get a reference to the view:

After creating your ViewController class (.h & .m) file, drag a ViewController into the Storyboard and type in this class name as this VC’s class.

You can then add a property in its .h file:

@property (weak) IBOutlet YOURVIEW *view;

Access it like this:

[YOURVIEWCONTROLLERCLASS view]

_

Load the ViewController from the nib (Make sure, you’Ve set the identifier in the storyboard):

YOURVIEWCONTROLLERCLASS *vcref = [self.storyboard instantiateViewControllerWithIdentifier:@"YOURVIEWCONTROLLERCLASS”];
[[vcref view] doSomething];

_

Use your AppDelegate to refer to its VCs. You shouldn’t really consider this. There aren’t many cases were this is actually useful. Go with 1 or 2.

Thomas Johannesmeyer
  • 2,834
  • 2
  • 34
  • 52