Ios UINavigationBar-将新控制器推送到UINavigationController堆栈时从未调用pushNavigationItem

Ios UINavigationBar-将新控制器推送到UINavigationController堆栈时从未调用pushNavigationItem,ios,Ios,今晚我要做一些测试,看看本机UINavigationBar的行为。我创建了一个简单的代码段,它执行以下操作: - (void)pushController { PHViewController *ctrl2 = [[[PHViewController alloc] initWithNibName:@"PHViewController" bundle:nil] autorelease]; ctrl2.shouldShowPrompt = YES; [self.viewCon

今晚我要做一些测试,看看本机
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:动画


无论如何谢谢你

这是一个解决方案,适用于任何像我一样挣扎的人。在使用自定义
UINavigationBar
的自定义
UINavigationController
子类中,使用以下代码覆盖
pushViewController
方法:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [super setViewControllers:[self.viewControllers arrayByAddingObject:viewController]
                     animated:animated];
}
然后在自定义
UINavigationBar
子类中,您可以自定义
navigationItems
如下所示:

- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated
{
    // customize your navigationItem here...

    [super pushNavigationItem:item animated:animated];
} 

谢谢不幸的是,
-(void)pushNavigationItem:(UINavigation*)项是私有的,这限制了导航栏的可定制性。如果是这样,我们是否必须覆盖
UINavigationController
的push和pop方法来调用导航栏的
pushNavigationItem:animated:
方法?我认为在导航控制器中使用
UINavigationBar
子类并不“非法”。毕竟,您可以在Interface Builder中轻松配置它。所以你的问题是绝对正确的。哪个部分不起作用?我现在正在应用程序中使用此代码。为我工作。干得好@lan hoar你觉得这个方法怎么样