Navigate with UINavigationController in iOS7

A UINavigationController is responsible for managing the navigation of hierarchical content. The navigation controller manages the current displayed screens using the navigation stack. At the bottom of this stack is the root view controller and at the top is the view controller currently displayed. You use methods to push and pop view controllers on and of the stack. In this tutorial we see an example. 

Open Xcode and create a new Empty Application. For product name, use iOS7NavigationControllerTutorial and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices. 

Create a new File. Choose Objective-C Class. Name this class iOSRootViewController which is a subclass of UIViewController.  The newly created class will be the root viewcontroller, let's hook this up in the AppDelegate. Declare the following property in the @interface section in AppDelegate.h.

@class iOSRootViewController

@property (strong, nonatomic) iOSRootViewController *rootViewController;

In AppDelegate.m import the header file.

#import "iOSRootViewController.h"

in didFinishLaunchingwithOptions, initialize the RootViewController and add it to the window. insert the following lines before the return YES line.

self.rootViewController = [[iOSRootViewController alloc] initWithNibName:nil bundle:nil]; 
[self.window addSubView: self.rootViewController.view];

Give the RootViewController a blue background. In iOSRootViewController.m in ViewDidLoad, insert the following line.

self.view.backgroundColor = [UIColor blueColor];

Build and Run.

In AppDelegate.h create a UINavigationController property

@property (strong, nonatomic) UINavigationController *navigationController;

In AppDelegate.m in the didFinishLaunchingWithOptions, hook the navigation controller up in the window. Remove the line.

[self.window addSubview: self.rootViewController.view];

Add the following lines

self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootViewController];
[self.window addSubview:self.navigationController.view];

Build and Run , you can see the navigation bar at the top of the screen.

Give the RootViewController a title. In iOSRootViewController.m in ViewDidLoad, insert the following line.

self.title = @"First Controller";

Build and Run, you now see the view controller's title in the navigation bar.

Now it's time to create the second ViewController. Create a new File. Choose Objective-C Class. Name this class iOSSecondViewController which is a subclass of UIViewController. Deselect the "With Xib for user interface" checkbox.

Change the background color and give the viewcontroller a title. In iOSSecondViewController.m in ViewDidLoad, insert the following lines

self.view.backgroundColor = [UIColor redColor]; 
self.title = @"Second Controller";

In iOSRootViewController.m import the secondViewController header file

#import "iOSSecondviewcontroller.h"

We create a button on the right side of the navigationbar. When we press this button the secondViewController will be pushed on the stack. In iOSRootViewController in the ViewDidLoad method insert the following line.

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Push" style:UIBarButtonItemStylePlain target:self action:@selector(pushSecondController)];

The pushSecondController method initializes the secondviewController. the pushViewController:animated method pushed the viewController on the stack.

- (void)pushSecondController 
  iOSSecondViewController *secondController = [[iOSSecondViewController alloc] initWithNibName:nil bundle:nil]; 
  [self.navigationController pushViewController:secondController animated:YES]; 

Build and Run, you can see you can switch between viewControllers.

You can download the source code of the iOS7NavigationControllerDemo at the ioscreator repository on github.