iOS - Navigation Bar

Advertisements


Use of Navigation bar

Navigation bar contains the navigation buttons of a navigation controller which is a stack of view controllers which can be pushed and popped. Title on the navigation bar is the title of the current view controller

Sample code and steps

1. Create a view based application

2. Now select the App Delegate.h and add a property for navigation controller as follows

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@property (strong, nonatomic) UINavigationController *navController;

@end

3. Now update the application:didFinishLaunchingWithOptions: method in AppDelegate.m file to allocate the navigation controller and makes it window's root view controller as follows

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:
    [[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] 
    initWithNibName:@"ViewController" bundle:nil];
    //Navigation controller init with ViewController as root
    UINavigationController *navController = [[UINavigationController alloc]
    initWithRootViewController:self.viewController];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;
}

4. Now add new class file TempViewController by selecting File -> New ->File... -> Objective C Class and then name the Class as TempViewController with subclass UIViewController.

5. Add a UIButton navButon in ViewController.h is as follows

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{    
    UIButton *navButton;
}
@end

6. Now add a method addNavigationBarItem and call the method in viewDidLoad.

7. Create a method for navigation item action.

8. We also need to create another method to push another view controller TempViewController.

9. The updated ViewController.m is as follows

// ViewController.m
#import "ViewController.h"
#import "TempViewController.h"
@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addNavigationBarButton];
    //Do any additional setup after loading the view, typically from a nib
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)pushNewView:(id)sender{
   TempViewController *tempVC =[[TempViewController alloc]
   initWithNibName:@"TempViewController" bundle:nil];
   [self.navigationController pushViewController:tempVC animated:YES];
}

-(IBAction)myButtonClicked:(id)sender{
   // toggle hidden state for navButton
   [navButton setHidden:!nav.hidden];
}

-(void)addNavigationBarButton{
   UIBarButtonItem *myNavBtn = [[UIBarButtonItem alloc] initWithTitle:
   @"MyButton" style:UIBarButtonItemStyleBordered target:
   self action:@selector(myButtonClicked:)];
   
   [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
   [self.navigationItem setRightBarButtonItem:myNavBtn];
   
   // create a navigation push button that is initially hidden
   navButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   [navButton setFrame:CGRectMake(60, 50, 200, 40)];
   [navButton setTitle:@"Push Navigation" forState:UIControlStateNormal];
   [navButton addTarget:self action:@selector(pushNewView:) 
   forControlEvents:UIControlEventTouchUpInside];
   [self.view addSubview:navButton];
   [navButton setHidden:YES];
}
@end

10. Now when we run the application we'll get the following output

iOS Tutorial

11. On clicking the navigation button MyButton, the push navigation button visibility is toggled

12. On click the push navigation button another view controller is pushed as shown below.

iOS Tutorial

Advertisements
Advertisements