Sunday, June 9, 2013

UINavigationBar pushNavigationItem example in Objective C (iOS).


UINavigationBar pushNavigationItem

Pushes the given navigation item onto the receiver’s stack and updates the navigation bar.

- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated

Parameters
item
The navigation item to push on the stack.
animated
YES if the navigation bar should be animated; otherwise, NO.

Discussion of [UINavigationBar pushNavigationItem]
Pushing a navigation item displays the item’s title in the center on the navigation bar. The previous top navigation item (if it exists) is displayed as a back button on the left side of the navigation bar. If the new top item has a left custom view, it is displayed instead of the back button.

UINavigationBar pushNavigationItem example.
On your navigationbar reference you could invoke the two methods:

- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated
- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated
In your custom view controller class write something like this:

self.navigationItem.leftBarButtonItem = yourButton;
self.navigationItem.rightBarButtonItem = yourSecondButton;
So you could write something like this:

[myNavBarReference pushNavigationItem:self.navigationItem animated:NO];

Example of [UINavigationBar pushNavigationItem].
Try using the navigation item's title property for this:

UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:@"Login"] autorelease];
[yourNavigationBar pushNavigationItem:item animated:NO];

UINavigationBar pushNavigationItem example.
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self 
action:@selector(toggleHelpGame)];
                        UINavigationItem *navigationItem = [[UINavigationItem alloc] 
initWithTitle:@"Help"];
                        navigationItem.rightBarButtonItem = buttonItem;
                        [navigationItem setLeftBarButtonItem:nil];
                        [self.sideNavigationBarCtrl pushNavigationItem:navigationItem 
animated:NO];
                        [navigationItem release];
                        [buttonItem release];

End of UINavigationBar pushNavigationItem example article.