gpt4 book ai didi

iOS - UINavigationController 并添加左栏按钮?

转载 作者:行者123 更新时间:2023-11-28 22:52:50 25 4
gpt4 key购买 nike

下面的方法是 UINavigationController 的委托(delegate)方法。我想成为决定是否将左侧项目添加到每个页面的人。下面的代码不起作用,我做错了什么吗?

我不想通过 ViewController 本身执行此操作。我希望 NavigationCopntroller 负责这项任务。

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:@selector(menuItemSelected:)];
[self.navigationItem setLeftBarButtonItem:menuItem];

// I also tried
// [viewController.navigationItem setLeftBarButtonItem:menuItem];
}

最佳答案

我认为问题是您试图访问 viewController 上的 navigationItem 属性,但它不存在,因为在 willShowViewController 方法中 viewController 是还没有在 navigationController 堆栈中,尝试使用 didShowViewController 委托(delegate)方法

像这样:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UIBarButtonItem *menuItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:@selector(menuItemSelected:)];
[self.navigationItem setLeftBarButtonItem:menuItem];

[viewController.navigationItem setLeftBarButtonItem:menuItem];

关于iOS - UINavigationController 并添加左栏按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11508197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com