Add subview using storyboard without external xib

2020-05-19 11:50发布

问题:

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.

回答1:

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];


回答2:

First create subview with design in some viewcontroller xib after that copy that view and paste in sub of ur viewcontroller in story board and give connections. Hope it will work.