Sunday, June 9, 2013

UINavigationBar popNavigationItemAnimated example in Objective C (iOS).


UINavigationBar popNavigationItemAnimated

Pops the top item from the receiver’s stack and updates the navigation bar.

- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated

Parameters
animated
YES if the navigation bar should be animated; otherwise, NO.

Return Value
The top item that was popped.

Discussion of [UINavigationBar popNavigationItemAnimated]
Popping a navigation item removes the top item from the stack and replaces it with the back item. The back item’s title is centered on the navigation bar and its other properties are displayed.

UINavigationBar popNavigationItemAnimated example.
#import "AppDelegate.h"

@implementation UINavigationBar (custom)
- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
{

    AppDelegate *delegate = [[UIApplication sharedApplication] delegate];

    [delegate.navController popViewControllerAnimated:NO];

    return TRUE;
}

@end

Example of [UINavigationBar popNavigationItemAnimated].
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {

if ([[navigationBar items] indexOfObject:item] == 1) {
    [expandedStack restack];   
}

if (!progPop) {
    progPop = YES;
    [navBar popNavigationItemAnimated:NO];
    return NO;
}

else {
    progPop = NO;
    return YES;
}
}

UINavigationBar popNavigationItemAnimated example.
To prevent the navigation bar from being animated it is not sufficient to override (UIViewController *)popViewControllerAnimated:(BOOL)animated. It is also necessary to create a custom navigation bar and override (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated:

- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated {
    return [super popNavigationItemAnimated:NO];
}

End of UINavigationBar popNavigationItemAnimated example article.