我今晚正在做一些测试来查看原生UINavigationBar的行为.我创建了一个简单的代码片段,它执行以下操作:
- (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; }
现在我已经将UINavigationController的UINavigationBar子类化了(我知道这是非法的,这是一个教育问题)我已经覆盖了以下方法:
- (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]; }
这是我的问题:为什么控制台中存在“弹出导航项”(因此被调用的方法)和“推送导航项”不是?
解决方法
我找到了原因:它调用 – (void)pushNavigationItem:(UINavigation *)项不调用 – (void)pushNavigationItem:animated!
还是非常感谢!