iPhone Development 101

iPhone 101

Code Tips

Resources

Links

Follow

Mailing ListTwitter

More

iPhone Development 101: User Interface: UINavigationController:
How to create a UINavigationBar Button

Right Button with an Image:

In your view controller's viewDidLoad method, add the following code:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
                    initWithImage:[UIImageimageNamed:@"house.png"] 
                            style:UIBarButtonItemStylePlain 
                           target:self 
                           action:@selector(goHome:)];
                            
    self.navigationItem.rightBarButtonItem = rightButton;
}

Also implement the selector method. For the above example:

-(void)goHome:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

There are plenty of icons you can download for use as nav bar buttons.

Button with Built-in image:

You can also create a button using one of the built-in UIBarButtonSystemItem icons:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
  initWithBarButtonSystemItem:UIBarButtonSystemItemSearch 
                       target:self 
                       action:@selector(doSearch:)];

self.navigationItem.rightBarButtonItem = rightButton;

Button with Text:

To add a plain text button:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
                initWithTitle:@"Code" 
                        style:UIBarButtonItemStylePlain 
                       target:self 
                       action:@selector(showDocco:)];

self.navigationItem.rightBarButtonItem = rightButton;

Additional References


TopHome