简体   繁体   中英

Back button doesn't appear in navigation bar

I'm having trouble understanding the way view controllers are presented and how to navigate between them in swift. I have a contact list, when you click on one contact it opens the related conversation, but it doesn't show any back button to the conversation list. My storyboard is : Contact List -> Navigation Controller -> Messages Controller

There is no navigation controller before contact list because it messes up with a sliding menu.

Here is my code:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "ShowConversationFromSelectedContact" {

        let nav = segue.destinationViewController as! UINavigationController
        let cdtvc = nav.topViewController as! MessageViewController
        cdtvc.currentConversation = openConversation

    }

}

More generally if you could explain or have any good ressource about the way stack of controllers work and how to navigate between them it would be great, I read a bunch articles but it's still unclear.

Re-arrange it so it is Nav Controller -> Contact list -> Message controller , and use push segues (if you're using swift 1.2+, they're called Show segues).

Inside prepareForSegue: you don't need to do anything else besides setting the contact id (or whatever data you need to pass) to the message controller.

Edit:

For a sliding menu, I suggest the following navigation model:

  1. The Storyboard ViewController s hierarchy goes

    Nav Controller --root segue--> Contact List VC --show segue--> Message VC

  2. Sliding menus are usually to present Top Level Views . Your Contact List VC is a top level view. Since it is also the root VC, you can use self.navigationItem.leftBarButtonItem to assign a button to show your sliding menu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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