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?
Asked
Active
Viewed 3,800 times
0
-
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) – Thomas Johannesmeyer Dec 27 '13 at 02:35
2 Answers
2
Give your view controller in IB an identifier then get it like this
MainViewController *mvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainController"];
More details here
![](../../users/profiles/-1.webp)
Community
- 1
- 1
![](../../users/profiles/231964.webp)
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.
![](../../users/profiles/1277898.webp)
Thomas Johannesmeyer
- 2,834
- 2
- 34
- 52