Question

I have ViewcontrollerA , which is a view made in storyboard. In this view, i have a UIView that i have added in storyboard and create an outlet to it called containerView.

I wants to add some other viewControllerB (also made in storyboard) to the container.

Tried that :

//add to container a new view from storyboard,with id called serviceView
UIViewController *sv = [self.storyboard instantiateViewControllerWithIdentifier:@"ServiceView"];
[self.containerView addSubview:sv.view];

and got a crash.

How can i do that ?

Thanks .

Was it helpful?

Solution

It most likely crashed because sv is nil, though you should have posted what reason it gave for the crash in your question. If this is the reason, it's probably because your storyboard doesn't contain a view controller with that storyboard identifier. But even if you fixed this, this isn't the proper way of doing view controller containment.

As for adding a view controller to another view controller, you need to use the view controller containment API added in iOS 5. In fact you can even use the storyboard to do view controller containment without writing code. From the Object Library, you can drag out a "Container View". It will let you attach another view controller from your storyboard graphically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top