简体   繁体   English

Swift 2:更改与标签栏控制器相关的视图上的导航标题

[英]Swift 2 : Change navigation title on a view related to a Tab Bar Controller

I want to change the navigation's title of a view, which is related to a Tab Bar Controller, which is related to a Navigation Controller (see the picture)我想更改一个视图的导航标题,它与一个标签栏控制器相关,它与一个导航控制器相关(见图)

I don't know how to do that.我不知道该怎么做。

With a basic view, I just need to do that in the ViewController.swift : self.title="test"使用基本视图,我只需要在 ViewController.swift 中执行此操作: self.title="test"

But here, this line changed the tab bar's title, but I want to change the navigation's title.但在这里,这一行改变了标签栏的标题,但我想改变导航的标题。

Main.Storyboard :主故事板:

使用需要用到这个属性:

self.navigationItem.title = "someTitle"

On Swift 3, in the UIViewController, override your viewDidAppear method and add this code snippet:在 Swift 3 中,在 UIViewController 中,覆盖您的 viewDidAppear 方法并添加以下代码片段:

if let tabController = self.parent as? UITabBarController { 
    tabController.navigationItem.title = "My Title"
}

According to Apple best practices you should not have a tab bar controller contained inside of a navigation controller, rather you should have the view controller for each tab that requires one to be inside of it's own navigation controller.根据 Apple 最佳实践,您不应该在导航控制器中包含标签栏控制器,而应该为每个需要一个位于其自己的导航控制器内部的选项卡设置视图控制器。

There are various issues that can arise from having a tab bar controller contained within a navigation controller.在导航控制器中包含标签栏控制器可能会出现各种问题。

When implemented according to their standards you can set the title using self.title根据他们的标准实施时,您可以使用 self.title 设置标题

An app that uses a tab bar controller can also use navigation controllers in one or more tabs.使用标签栏控制器的应用程序也可以在一个或多个标签中使用导航控制器。 When combining these two types of view controller in the same user interface, the tab bar controller always acts as the wrapper for the navigation controllers .当在同一个用户界面中组合这两种类型的视图控制器时,标签栏控制器始终充当导航控制器的包装器

https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/CombiningViewControllers.html

将 UINavigationController 嵌入您的故事板并放置:

navigationBar.topItem.title = "Nav title"

I also had difficulty to change a navigation bar title of a child view controller.我也很难更改子视图控制器的导航栏标题。 The solution was:解决方案是:

@IBOutlet weak var navigationBar: UINavigationBar!

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationBar.topItem!.title = "Pickup Address"
}

For Swift 3+ Swift 3+

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    self.navigationItem.title = "Title"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM