×

iOS Navigation Controller


What is UINavigationController ?

UINavigationController is used to handle all the navigation . Assume it like header of your app which includes title ,backButton and mauch more.

Example Output :-


When you click on Push button


Create a New Project as always or use your existing project .

Write this code in AppDelegate.m didFinishLaunching Application

Example :-

Import this file at top after #import "AppDelegate.h" #import "ViewController.h"


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    ViewController *abcController = [[ViewController alloc]init];
    
    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:abcController];
     self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;
}

Firstly we allocated ViewController reference "abcController" because we want this viewController should start first among 100's of thousands of ViewController's in our Code. You can choose which viewController you want your app should start first. Then we allocated UINavigationController and initialised it with abcController because we want to add a Navigation bar to ViewController Screen . And with the help of window's rootViewController property we initialised it with navController. Make sure you import "ViewController.h" or whatever viewController you want to start with at top of file.

Remove all the code from ViewController.m and add this fresh code

// Created by Manish Methani 
//  Copyright © 2017 Codzify. All rights reserved.
//

#import "ViewController.h"
/* You may see error at this line . Ignore it for now. */
#import "SecondViewController.h"    

#define PaddingX 12

@interface ViewController ()
{
}
@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
     /* All about Navigation Controller Title , Back Button */ 
    NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
    
     /* give Background color to navigation bar */ 
    if ([[ver objectAtIndex:0] intValue] >= 7) {
        // iOS 7.0 or later
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];;
        self.navigationController.navigationBar.translucent = NO;
    }else {
        // iOS 6.1 or earlier
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];;
   
    }
    
     /* give title to navigation bar */ 
    self.navigationItem.title = @"First View Controller";
    
    /* give titleColor to Navigation Title */ 
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    
     /* Button Push */ 
    UIButton *buttonPushToAnotherViewController = [[UIButton alloc]initWithFrame:CGRectMake(32, CGRectGetMidY(self.view.frame)-40, 320, 60)];
    [self.view addSubview:buttonPushToAnotherViewController];
    [buttonPushToAnotherViewController setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonPushToAnotherViewController setTitle:@"Click to Push >" forState:UIControlStateNormal];
    [buttonPushToAnotherViewController addTarget:self action:@selector(buttonPushClicked) forControlEvents:UIControlEventTouchUpInside];
    buttonPushToAnotherViewController.backgroundColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];
    buttonPushToAnotherViewController.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0];
    buttonPushToAnotherViewController.layer.cornerRadius = 4.0;
}

 #pragma mark - Actions 
-(void)buttonPushClicked
{
    SecondViewController *secondViewController = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondViewController animated:YES];
}

In the viewDidLoad Method , how to change the backgroundColor of NavigationBar , textColor of Title , how to give title to navigation Bar. Each line is self explanatory with Comments. But make sure you add backgroundColor to your View Otherwise you will see black screen in your App.Then we initialised Push Button with the help of which we move to another VewController as you see in final Output.

How to move from one ViewController to AnotherViewController ?

Suppose you have two view Controllers named as firstViewController and secondViewController. Now if you want to move from first to second viewController you have to use UINavigationController's pushViewController property. As shown in Final Output image , you may find pushButton on the screen and when you click on that pushButton secondviewController Opens . You may go back to firstViewController again with the help of backButton .

Syntax :

[self.navigationController pushViewController:viewControllerNameWhichIsGoingToBePushed animated:YES];

viewControllerNameWhichIsGoingToBePushed :- This is the name of your viewController which you want to push.

How to use pushViewController in Code ?

See in FirstViewController.m file we had written Push Action Method

-(void)buttonPushClicked
{
 SecondViewController *secondViewController = [[SecondViewController alloc]init];

 /* how to push to another View Controller */  
 [self.navigationController pushViewController:secondViewController animated:YES];
}

On Action of PushButton we told our self.navigationController to push to secondViewController.

Setup SecondviewController Now


Goto File , New File > CocaTouch Class > Enter FileName as SecondViewController



Remove all the code from SecondViewController.m and add this fresh code

//  SecondViewController.m
//  Created by Manish Methani 
//  Copyright © 2017 Codzify. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
 /* All about Navigation Controller Title , Back Button */ 
    NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
    
    /* give Background color to navigation bar */ 
    if ([[ver objectAtIndex:0] intValue] >= 7) {
        // iOS 7.0 or later
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];;
        self.navigationController.navigationBar.translucent = NO;
    }else {
        // iOS 6.1 or earlier
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];;
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];;
    }
    
     /* give title to navigation bar */ 
    self.navigationItem.title = @"Second View Controller";
    
    /* give titleColor to Navigation Title */ 
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor]}];


    /* give left Button to Navigation Bar */ 
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"< Go Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonClicked)];
   
    /* Chnage TextColor of Left Or Right BarButton on Navigation Item */
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    
    self.navigationItem.leftBarButtonItems = @[item];
    
    self.view.backgroundColor = [UIColor whiteColor];
                            
    UILabel *secondLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, CGRectGetMidY(self.view.frame)-80, 320, 60)];
    [self.view addSubview:secondLabel];
    secondLabel.text = @"You are on the second View Controller";
    secondLabel.textColor = [UIColor colorWithRed:0/255.0 green:169/255.0  blue:98/255.0 alpha:1.0];
    secondLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
    secondLabel.numberOfLines = 0;
    secondLabel.lineBreakMode = NSLineBreakByWordWrapping;
    
}

#pragma mark - Actions
-(void)backButtonClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end

In SecondViewController we added one label to indicate wether we are on the second screen or not .