【问题标题】:iOS - UINavigationController and adding a left bar button?iOS - UINavigationController 并添加左栏按钮?
【发布时间】:2012-07-16 16:07:46
【问题描述】:

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

我不想通过 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];
}

【问题讨论】:

    标签: ios uinavigationcontroller uibarbuttonitem


    【解决方案1】:

    我认为问题在于您正在尝试访问 viewController 上的 navigationItem 属性,但它不存在,因为在 willShowViewController 方法中 viewController 尚未在 navigationController 堆栈中,请尝试使用didShowViewController委托方法

    像这样:

    - (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]; 
    

    }

    【讨论】:

      【解决方案2】:

      控制台中有一条警告说“嵌套推送可能损坏导航控制器” 我将 2 个 ViewController 推入堆栈,而不是一次推 1 个 ViewController。

      解决此问题并消除警告也解决了此问题。

      【讨论】:

        猜你喜欢
        • 2014-03-20
        • 1970-01-01
        • 1970-01-01
        • 2011-07-15
        • 1970-01-01
        • 2015-09-02
        • 1970-01-01
        • 1970-01-01
        • 2015-08-03
        相关资源
        最近更新 更多