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?

  • Are you using a .storyboard file? Slightly confusing as to what you've done. – Sam Jarman Dec 27 '13 at 02:20
  • possible duplicate of [Storyboard - refer to ViewController in AppDelegate](http://stackoverflow.com/questions/8186375/storyboard-refer-to-viewcontroller-in-appdelegate) – Git.Coach Dec 27 '13 at 02:35

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,277
  • 15
  • 55
  • 100
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.

Git.Coach
  • 3,032
  • 2
  • 37
  • 54