Add subview using storyboard without external xib

GuyFox picture GuyFox · Nov 28, 2011 · Viewed 11.5k times · Source

I'm learning Objective-C.

I have a problem with new storyboard feature. I would initialize a subview inside a main view with xib.

Without using storyboard, I could do it using:

controller = [[UIViewController alloc]initWithNibName:@"NibName" bundle:nil];
[self.view addSubview:controller.view];

Now, I wouldn't use an external xib, but I want manage a view in a class and use it like a subview in another class.

I know that is possible use a xib and use a similiar code to load it, but it must be out the storyboard.

Answer

django-d picture django-d · Nov 29, 2011

First create the view in your storyboard and then instantiate it with the following code. Also make sure you give it an identifier via the Attributes Inspector.

controller = [self.storyboard instantiateViewControllerWithIdentifier:@"identifier"];
[self.view addSubview:controller.view];