Road To iOS Series 4

September 6, 2014

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

 600 480 navigation interface overview

image source

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

UINavigationController view components


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;

 600 400 segue

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:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
	// check the segue id we set in storyboard
    if ([segue.identifier isEqualToString:@“DoSomething”]) {
    	// check if the destination is correct by checking class reference
        if ([segue.destinationViewController isKindOfClass:[DoSomethingVC class]]) {
            DoSomethingVC *doVC = (DoSomethingVC *)segue.destinationViewController;
            	// e.g. doVC.date = getDateFromSomewhere(...);
				doVC.neededInfo = ...;
		}
	}
}

Push, Pop lifecycle

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

  1. Stanford iOS 7 development
  2. Mac Developer Library
  3. How a Navigation-based App Fits Together
  4. UINavigationController Class Reference
  5. View Controller Programming Guide for iOS