Dismiss pushed view controller

KyloR picture KyloR · Mar 24, 2017 · Viewed 11.2k times · Source

So I have a view controller which I display as follows:

func showProfileForTrainer(trainer: Trainers) {
    let viewPTProfileVC = ViewPTProfileVC()
    viewPTProfileVC.trainer = trainer
    self.navigationController?.pushViewController(viewPTProfileVC, animated: true)
}

But when trying to dismiss the view, I cannot get it to work. It has a back button in a navigation bar which functions fine, but when trying to dismiss the view via a button for example, it does nothing. I have currently tried:

func handleMessageTrainer() {
    dismiss(animated: true) {
        print(1)
        self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
    }
    self.dismiss(animated: true) {
        print(2)
        self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
    }
    navigationController?.dismiss(animated: true, completion: {
        print(3)
        self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
    })
    self.navigationController?.dismiss(animated: true, completion: {
        print(4)
        self.tabBarVC?.showChatLogForTrainer(trainer: self.trainer!)
    })
    print(5)
}

As you can see I have tried varying ways and none work, and the console just outputs 5.

Frustratingly, elsewhere in my app I presented a view in the same way as shown at the beginning and it dismissed fine using dismiss(animated: true) { ... }

Any idea why it won't work here?

Answer

Paulo Mattos picture Paulo Mattos · Mar 24, 2017

You must pop the view controller from the corresponding navigation controller:

self.navigationController?.popViewController(animated: true)