UINavigationBar -pushNavigationItem is never called when a new controller is pushed onto the UINavigationController stack

Today I will conduct a test to look at the behavior of the native UINavigationBar . I created a simple piece of code that does the following:

 - (void)pushController { PHViewController *ctrl2 = [[[PHViewController alloc] initWithNibName:@"PHViewController" bundle:nil] autorelease]; ctrl2.shouldShowPrompt = YES; [self.viewController pushViewController:ctrl2 animated:YES]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. PHViewController *ctrl = [[[PHViewController alloc] initWithNibName:@"PHViewController" bundle:nil] autorelease]; ctrl.shouldShowPrompt = YES; ctrl.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Push" style:UIBarButtonItemStyleDone target:self action:@selector(pushController)] autorelease]; self.viewController = [[[PHNavigationController alloc] initWithRootViewController:ctrl] autorelease]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } 

Now I have subclassed the UINavigationBar this UINavigationController (I know this is illegal, this is an educational question), and I have redefined the following methods:

 - (void)setItems:(NSArray *)items animated:(BOOL)animated { NSLog(@"Setting Navigation Item"); [super setItems:items animated:animated]; } - (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated { NSLog(@"Pushing Navigation Item"); [super pushNavigationItem:item animated:animated]; } - (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated { NSLog(@"Poping Navigation Item"); return [super popNavigationItemAnimated:animated]; } - (void)setValue:(id)value forKeyPath:(NSString *)keyPath { NSLog(@"Setting Value: %@ for keyPath:%@", value, keyPath); [super setValue:value forKeyPath:keyPath]; } 

Here is my question: why is the "Navigation Point Guidance" present in the console (hence the method being called) and the "Press Navigation Element" not?

+7
source share
2 answers

I found a reason: It calls - (void) pushNavigationItem: (UINavigation *) an element that does not call - (void) pushNavigationItem: animated!

Anyway, thanks!

+11
source

Here is a solution for those who fought with him, like me. In a custom subclass of UINavigationController that your custom UINavigationBar uses, it overrides the pushViewController method with the following code:

 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { [super setViewControllers:[self.viewControllers arrayByAddingObject:viewController] animated:animated]; } 

and then in your custom subclass of UINavigationBar you can configure your navigationItems like this:

 - (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated { // customize your navigationItem here... [super pushNavigationItem:item animated:animated]; } 
+1
source

All Articles