thatnaoki
thatnaoki

Reputation: 3

UINavigationBar's title doesn't change with transition by push segue

So, in my project, I'm trying to use "push" segue when the button on the first screen titled "A" is tapped. The transition with push segue to the second screen titled "B" always goes well but navigation bar title doesn't change with the transition. On "B" screen, I name VC "B" on IBInspector so that the title of the second screen should be "B". But after the transition, the title is still "A" as below image. https://thatnaoki.sakura.ne.jp/image/navBar.gif

The project belongs to a company I work for so I cannot show the code or more detail information but I'm trying to tell you as detailed as I can. The first screen is appeared by segue of "present modally". I set the navigation controller as root vc of "A" screen.

Here is what I've tried so far.

I tried to create the same situation outside of this project. But I cannot reproduce this outside of the project. This means my project's codes must have a reason for this situation. But I'm totally not sure about which part is causing.

I read the apple documentation about navigation controller but they say "Each time the top-level view controller changes, the navigation controller updates the navigation bar accordingly." This doesn't help me so much.

So, I'm kind of stuck in this situation...

Could anybody give me any possible reason why navigation title doesn't change with the transition? I'm really sorry for not giving the example codes.

Upvotes: 0

Views: 873

Answers (2)

Roger
Roger

Reputation: 7612

Inside your View Controller:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.        
    if segue.identifier == "yadadada" {
        if  let nc = segue.destination as? UINavigationController {
            if let ti = nc.navigationBar.topItem {
                ti.title = "Yeah!"
            }
        }
    }        
}

Upvotes: 1

PGDev
PGDev

Reputation: 24341

Try setting the title with viewController’s title property in viewDidLoad()

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Your Title Here"
    }
}

Upvotes: 1

Related Questions