Please clear some confusions regarding UIViewController
I found this article Abusing UIViewController
and here are the links link1 & link2
and summarised points
This is author's (and Apple’s) advice In a nutshell:
- One (and only one) view controller should be responsible for a whole hierarchy (or screenful) of UIViews.
- Mostly, you should only use one view controller per screen. Essentially the
rootViewController
of the currentUIWindow
should be the onlyUIViewController
with a visible view. - Each different screen should have a different view controller i.e. one controller should not control more than one screen.
- You should NOT nest custom
UIViewControllers
within a view hierarchy. - If more than one
UIViewController
hangs off the application’sUIWindow
, only one of these will get the messages for changes in orientation. The other(s) will NOT get these messages. - Nested
UIViewControllers
are not guaranteed, or likely, to receive messages for changes in orientation or lifecycle messages such asviewDidAppear:
,viewWillAppear:
,viewDidDisappear:
andviewWillDisappear:
even though they inherit fromUIViewController
. Only the topmostUIViewController
is certain to get these messages.
Please clear point number 2 and 3
because when we use UINavigationController
or UITabBarController
we use multiple subclasses of UIViewController
. And ios device has only one screen.....
This article Abusing UIViewController highlight apple suggestion
Note: If you want to divide a view hierarchy into multiple subareas and manage each one separately, use generic controller objects (custom objects descending from NSObject) instead of view controller objects to manage each subarea. Then use a single view controller object to manage the generic controller objects.
and in apple docs under heading of Coordinating Efforts Between View Controllers
apple saying
Few iOS apps show only a single screenful of content. Instead, they show some content when first launched and then show and hide other content in response to user actions. These transitions provide a single unified user interface that display a lot of content, just not all at once.....
My requirement is NOT to use any container or modal or popover, I want to do manual management, I have two view controllers VC1
& VC2
. VC1
is the root view controller now I want to switch/transit/move to VC2
what should I do?
VC1
should be the only subclass ofUIViewController
andVC2
should be the subclass ofNSObject
to manage a particular view inVC1
hierarchy?(the show hide thing by apple doc).VC2
can also be the subclass ofUIViewController
, I just removeVC1
from root view and addVC2
as root view?
or what is the correct way?
Container view controllers (like UINavigationController) allow working around the one-VC-per-screen rule. Since iOS 5, developers have been able and allowed to write our own container controllers (which aren't actually much different from normal VCs). Generally this means that writing non-VC controller objects is less necessary than it used to be.
In your situation, where you want to replace the root view controller, your option 2 makes more sense. Use VCs where you can, and non-VC controller objects only when you can't. Since you're replacing the whole screen's content, just switching the
UIWindow rootViewController
makes the most sense (edit: alternately, many devs would just use a navigation controller to present the second view, because it's simple and convenient).