Lemmy
Lemmy

Reputation: 59

Change title of UINavigationBar

I've created a view-based application. I've placed a Navigation Bar on the view and two buttons. I'd like to change the title of the nav bar after pressing the buttons. In IB I've connected the buttons with the corresponding actions. Debugging shows that the actions are getting called but the title of the nav bar is not changed.

Kindly help!

//
//  NavBarViewController.h

#import <UIKit/UIKit.h>

@interface NavBarViewController : UIViewController {

}

-(IBAction) clickedOne: (id)sender;
-(IBAction) clickedTwo: (id)sender;

@end
//
//  NavBarViewController.m

#import "NavBarViewController.h"

@implementation NavBarViewController

-(IBAction) clickedOne: (id)sender {
    self.navigationItem.title = @"1";
}

-(IBAction) clickedTwo: (id)sender {
    self.navigationItem.title = @"2";
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

Upvotes: 0

Views: 549

Answers (1)

Lemmy
Lemmy

Reputation: 59

I've found it. I've used a UINavgationItem and connected the IBOutlet in IB with UINavigationItem.

//
//  NavBarViewController.h

#import <UIKit/UIKit.h>

@interface NavBarViewController : UIViewController {
  UINavigationItem *navBar;
}

@property (nonatomic, retain) IBOutlet UINavigationItem *navBar;

-(IBAction) clickedOne: (id)sender;
-(IBAction) clickedTwo: (id)sender;

@end

//
//  NavBarViewController.m

#import "NavBarViewController.h"

@implementation NavBarViewController

@synthesize navBar;

-(IBAction) clickedOne: (id)sender {
    navBar.title = @"1";
}

-(IBAction) clickedTwo: (id)sender {
    navBar.title = @"2";
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

Upvotes: 1

Related Questions