Search code examples
swiftuitableviewsegue

connect segue to Navigation control


Before I used a Segue to transfer data between to view controllers.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Edit List" {
        if let currentTodo = todoReadyForEditing, let editControler = segue.destination as? ListPageController {
            editList.prepare(for: currentList)
        }

Now the plan is changed I should transfer the data from the first view controllers to a tableViewController. Since I want to have a navigation bar to the tableViewController page, I added a navigationBarController before it.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "Edit List" {
        if let currentTodo = todoReadyForEditing, let editControler = segue.destination as? ListTableViewController {
            editList.prepare(for: currentList)
        }

The problem is when I connected segue directly to UITableViewController, everything is work, same as before that I have two view controllers, but the navigation bar is hidden, which I don't want. As I read, apparently i have to connect the segue first to the navigationViewController, but when I did not, the data won't transfer to the table view controller.

Is there any suggestion? Thanks for your help in advance


Solution

  • Destination of segue is UINavigationController, but you can work with its topViewController property which is your controller

    guard let navCon = segue.destination as? UINavigationController, 
          let controller = navCon.topViewController as? ListTableViewController else { fatalError("Segue wasn’t set right") }
    

    Anyway, I would suggest you don’t use segues and create your UINavigationController with embedded controller programmatically