如何在NavigationController之后在TabBar连接的ViewController中显示navigationItem.title

时间:2015-11-09 15:58:03

标签: ios swift tabbar navigationbar

很抱歉这个令人困惑的标题。我的设置就像下面这样简单:

`Navigation Controller -> View Controller1 -> Tab Bar Controller -> View Controller2
                                                                 -> View Controller3`

有一个show-segue,由ViewController1中的BarButtonItem触发,它指向TabBarController。对于View Controller 2& 3(连接到标签栏控制器),我配置了标题并在右上角添加了一个BarButtonItem。这就是全部。当我运行应用程序时,即使我直接在.swift-Class中设置标题,也不会显示标题VC2和3的按钮: (self.navigationItem.title = "title of ViewController2"

相反,模拟器显示的标题只有'<回' VC2& 3中的按钮。

我想要的是'<回'按钮+我的标题和右边的BarButtonItem。很抱歉提出这样一个微不足道的问题,但我无法找到解决方案。

我使用的是Swift 2.1,Xcode 7.1,OS X 10.11.1 谢谢!

Storyboard-Screenshot

1 个答案:

答案 0 :(得分:1)

所以,我终于为我的案子找到了解决方案。 ViewController1可以在prepareForSegue方法中发送VC2& 3的标题,如:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let vc = segue.destinationViewController.navigationItem.title = "new title"
}

或者更好的解决方案:ViewController2设置自己的标题。

class ViewController2: UIViewController {
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        navigationController?.visibleViewController?.navigationItem.title = "new title"
    }
}