3

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?

4
  • 1
    I think the confusion here is the difference between presenting a view and the order in which they are presented. addSubview adds the view the end of the list of views. Meaning it will be layered in the order they are added see developer.apple.com/library/ios/documentation/WindowsViews/… bringSubviewToFront and presentViewController manipulates the position of the view
    – Vincent F
    Commented Jun 15, 2015 at 16:11
  • 1
    You are devising an architecture of view controllers. Your job is to come up with one that's appropriate. Cocoa includes some built-in parent view controllers that help with common architectural structures, or you can build your own. Do what will be best for the user. See my book: apeth.com/iOSBook/ch19.html
    – matt
    Commented Jun 15, 2015 at 16:12
  • @vfaller... I'm confused about why each of the techniques can effectively PRESENT the view for use by the user. For example, if adding a subview is just as effective as presentViewController then why would you ever need to use presentViewController ?? Commented Jun 15, 2015 at 16:16
  • 1
    @user3055655: What you use depends on what you are trying to accomplish (including not writing code at all). Note that your first code snippet may be wrong as it does not maintain the view controller object graph correctly (it depends on what code you've left out of your snippet). If you have not done so already, you must read the View Controller Programming Guide for iOS (developer.apple.com/library/ios/featuredarticles/…). This should answer most of your questions about Why use View Controllers? Commented Jun 15, 2015 at 16:22

1 Answer 1

9

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.