草庐IT

ios - UINavigationBar -pushNavigationItem 在将新 Controller 推送到 UINavigationController 堆栈时从不调用

coder 2023-09-21 原文

我今晚正在做一些测试,以查看 native 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;
}

现在我已经子类化了 UINavigationControllerUINavigationBar(我知道这是非法的,这是一个教育问题)并且我已经覆盖了以下方法:

- (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 *)item 而没有调用 - (void)pushNavigationItem:animated!

还是谢谢你!

关于ios - UINavigationBar -pushNavigationItem 在将新 Controller 推送到 UINavigationController 堆栈时从不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7356459/

有关ios - UINavigationBar -pushNavigationItem 在将新 Controller 推送到 UINavigationController 堆栈时从不调用的更多相关文章

随机推荐