Sunday, June 9, 2013

UINavigationItem setRightBarButtonItem example in Objective C (iOS).

UINavigationItem setRightBarButtonItem

Sets the custom bar button item, optionally animating the transition to the view.

- (void)setRightBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated

Parameters of [UINavigationItem setRightBarButtonItem]
A custom bar item to display on the right of the navigation bar.
Specify YES to animate the transition to the custom bar item when this item is the top item. Specify NO to set the item immediately without animating the change.

Discussion of [UINavigationItem setRightBarButtonItem]
If two navigation items have the same custom left or right bar button items, those bar button items remain stationary during the transition when the navigation item is pushed or popped.

UINavigationItem setRightBarButtonItem example.
UINavigationItem *backNavItem = [[UINavigationItem alloc] initWithTitle:@"TESTTEST"];
[backNavItem setRightBarButtonItem:backButton animated:YES];

Example of [UINavigationItem setRightBarButtonItem].
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(theEditMethod:)];    
[viewController.navigationItem setLeftBarButtonItem:leftBarButton animated:NO];
[leftBarButton release];

UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(theAddMethod:)];     
[viewController.navigationItem setRightBarButtonItem:rightBarButton animated:NO];
[rightBarButton release];

End of UINavigationItem setRightBarButtonItem example article.