Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple ways to present UIViewController's view [closed]

I'm fairly new to iOS and working on someone else's project, and I notice that there seems to be various different ways to present a view. I'm wondering if someone could give a brief summary of the various ways to present a view, and when to use which one, because they all seem to work fine.

For example, one piece of code I've come across does something like this:

[parentView addSubview:childViewController.view];
[parentView bringSubviewToFront:childViewController.view];

Using LLDB to examine the childViewController, both of these return nil:

po self.parentViewController (this is nil)

po self.presentingViewController (this is nil)

In other places, I've seen things like this:

[parentView addSubview:childViewController.view];
[self addChildViewController:childViewcontroller];

Further still, in other places I've seen things like this:

UIViewController *childViewController = [[SomeChildController alloc] init];
[self presentViewController:childViewController animated:YES completion:^(void){}];

So sometimes you can have a parent view controller or presenting view controller if you wish, and other times it's not needed. With so many options to present views, how do you decide one over another, or which is right when?

like image 689
Logicsaurus Rex Avatar asked Jul 25 '26 23:07

Logicsaurus Rex


1 Answers

Really there are only 2 options to display ViewControllerB from ViewControllerA, and which you use will depend on what you want to achieve -

Option 1: Modal presentation

Use this if ViewControllerB should take focus away from ViewControllerA entirely until it is dismissed.

[self presentViewController:viewControllerB animated:YES completion:nil];

There are a few different modalPresentationStyles you can use or you can create your own transitions. But what's essential is that ViewControllerB will take focus away from ViewControllerA, and ViewControllerA can only present one view controller at a time. ViewControllerB's presentingViewController will point at ViewControllerA and ViewControllerA's presentedViewController will point at ViewControllerB.

UIKit Example: UIAlertController should be presented modally.

Option 2: View Controller containment

Use this if you wish to display ViewControllerB's view as part of ViewControllerA's view's hierarchy of subviews.

[self addChildViewController:viewControllerB];
[self.view addSubview:viewControllerB.view];
[viewControllerB didMoveToParentViewController:self];

Use this technique if you want ViewControllerB to only take up a portion of ViewControllerA's view, or if you want to display other view controllers' views from ViewControllerA too. ViewControllerB's parentViewController will point at ViewControllerA and ViewControllerA's childViewControllers array will include ViewControllerB.

UIKit Example: UINavigationController uses view controller containment to display its viewControllers.

like image 87
johnpatrickmorgan Avatar answered Jul 28 '26 00:07

johnpatrickmorgan