Multiple MVCs
Two multiple MVC controllers
- UINavigationController
- often used for a more detailed view (e.g. calendar in iOS)
- UITabBarController
- often used for views have no logical connections (e.g. timer in iOS)
What is UINavigationController
Whenever an iOS app displays a user interface, the displayed content is managed by a view controller or a group of view controllers coordinating with each other. Therefore, view controllers provide the skeletal framework on which you build your apps.
- It’s a View Controller manages stacks of other view controllers
- An MVC’s view is another MVC
If user wants to see more detail information about current view, navigation controller will push the view from root view->list view->detail view.
How does UINavigationController work
Overview
- Segue: one MVC goes to the other MVCs, or a visual transition from one scene to another
- Independent MVC: each MVC is independent, it’s encapsulated in its own view controller
- No memory between MVCs: everytime we push(segue) to another MVC, we create a new one, old one gets dealloc from the heap. You may need variables to store information if you want persistent data between segues
- When you create variables to store information, outlets in new view is not set yet.
Detail
A navigation controller is a container view controller—that is, it embeds the content of other view controllers inside of itself. You access a navigation controller’s view from its view property. This view incorporates:
- navigation bar
- an NSArray of UIBarButtonItems
- the back button will be generated automatically
- optional toolbar
- an NSArray of UIBarButtonItems
- optional, not necessary for every content view controller
- content view
- create your own
- by subclassing either the UIViewController class or the UITableViewController class
How does UINavigationController work (Cont.)
Push the view to screen
- often, in Xcode, we use push segue and along with its identifier to connect different MVCs
- however, you can do this programmatically with
- (void)pushViewControllerAnimated:(BOOL)animated;
Take the view off screen
- often, user hits the back button on the top left of the screen to get back to the parent view
- however, you can do this programmatically with
- (void)popViewControllerAnimated:(BOOL)animated;
Prepare for the segues
The segue offers the source VC the opportunity to “prepare” the new VC to come on screen. This method is sent to the VC that contains the button that initiated the segue:
Push, Pop lifecycle
Conclusion
- UINavigationController is container view controller to manage other view controllers(MVCs)
- Each MVC in UINavigationController is well encapsulated and independent
- UINavigationController mainly consists of three parts: navigation bar, navigation toolbar, content views(other MVCs)
- UINavigationController uses push segue and pop segue to make transition between content views(other MVCs)
- Segues use prepareForSegue to prepare data between different pushes
Reference
- Stanford iOS 7 development
- Mac Developer Library
- How a Navigation-based App Fits Together
- UINavigationController Class Reference
- View Controller Programming Guide for iOS