How to add a right button to a UINavigationController?

In this tutorial, going to explain how we can add right Bar Button Item in the UINavigationController. This can be done in a simple way with a few lines of code.

Step 1 :  Need to create a UIBarButtonItem Object

//Creating a UIBarButtonItem 
 UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Reload" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
 

Step 2 :

//adding the button to the UINavigationBar right side
self.navigationItem.rightBarButtonItem = rightBarButton ;

Step 3 :

//releasing the allocated Right bar button. 
[rightBarButton release];

Finally we need to write the function which is bind to the button to perform action

-(IBAction)refreshPropertyList:(id)sender{

// specify what we need to perform here when click the UINavgationBarButtonItem. 

}

UIBarButtonItem Code snippet

- (void)viewDidLoad {
  [super viewDidLoad];

 UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Reload" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];    
//adding the button to the UINavigationBar right side
self.navigationItem.rightBarButtonItem = rightBarButton ;
//releasing the allocated Right bar button. 
[rightBarButton release];

}
-(IBAction)refreshPropertyList:(id)sender{

// specify what we need to perform here when click the UINavgationBarButtonItem. 

}

Leave a Reply

Your email address will not be published. Required fields are marked *