Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add subview using storyboard without external xib

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.

like image 474
GuyFox Avatar asked Nov 28 '11 22:11

GuyFox


People also ask

How do I add a subview to a storyboard?

Select the subview and drag over the view entry in the Objects Explorer of storyboard. Once you leave the mouse hold, the parent view will have a triangle indicating your subview has become its child and the subview will have a bigger indent than your view.

Which is better storyboard or Xib?

If you are a single developer, it is good to use storyboard because it consumes less time. If the team consists of many developers, use xib, otherwise, it is not easy to merge the modules/tasks.


1 Answers

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];
like image 166
django-d Avatar answered Oct 12 '22 02:10

django-d