Reputation: 2798
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.
Upvotes: 0
Views: 2095
Reputation: 16288
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:
The Storyboard ViewController
s hierarchy goes
Nav Controller --root segue--> Contact List VC --show segue--> Message VC
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.
Upvotes: 2