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?
presentViewController
then why would you ever need to usepresentViewController
??View Controller Programming Guide for iOS
(developer.apple.com/library/ios/featuredarticles/…). This should answer most of your questions aboutWhy use View Controllers?