Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Nested View Controllers view inside UIViewController's view?

Is it typically bad programming practice in iOS to have a nested view controller's view inside UIViewController's view? Say for instance I wanted to have some kind of interactive element that responds to user's touches, but only takes up maybe 25% of the screen.

I suppose I would add this nested view controller to my UIViewController by saying something like:

[self.view addSubview: nestedViewController.view]; 
like image 532
Skyler Avatar asked Jul 06 '13 03:07

Skyler


People also ask

How do I present a view controller from another view controller?

Start a segue from any object that implements an action method, such as a control or gesture recognizer. You may also start segues from table rows and collection view cells. Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present.

How do I connect two view controllers in storyboard?

You need to create a segue from the viewController itself to the destination. You can do this for each viewController and give them appropriate identifiers.


2 Answers

No, this is generally good design, it helps keep your view controllers concise. However you should be using the view controller containment pattern, take a look at the following documentation.

Implementing a Container View Controller

This is incredibly simple to setup using Interface Builder with Storyboards as well, take a look at the Container View in the object library.

Here is a contrived example in a Storyboard. In this example you would have 4 view controllers, one that holds the 3 containers, and one for each container. When you present the left most controller that has all of the containers, the Storyboard will automatically initialize and embed the other 3. You can access these child view controllers via the childViewControllers property or there is a method you can override prepareForSegue:sender: and capture the destination view controllers of the segue about to be called. This is also a good point to pass properties to the child view controllers if any are needed.

enter image description here

like image 70
Chris Wagner Avatar answered Sep 21 '22 19:09

Chris Wagner


I put this code in the parent view controller. It works great for me.

Obj C

-(void)viewDidLoad{      [super viewDidLoad];      InnerViewController *innerViewController = [self.storyboard instantiateViewControllerWithIdentifier:INNER_VIEW_CONTROLLER];      [self addChildViewController:innerViewController];      [self.view addSubview:innerViewController.view];      [innerViewController didMoveToParentViewController:self]; } 

Swift:

 let childViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ChildViewController"),  self.addChildViewController(childViewController)  self.view.addSubview(childViewController.view)  childViewController.didMove(toParentViewController: self) 

Another option is to use IB and put container view. UIViewController will show up automatically (XCode 9 in this case): enter image description here

like image 21
Mobile Developer Avatar answered Sep 20 '22 19:09

Mobile Developer