Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss pushed view controller

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?

like image 657
Doto Pototo Avatar asked Mar 24 '17 18:03

Doto Pototo


2 Answers

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

self.navigationController?.popViewController(animated: true)
like image 111
Paulo Mattos Avatar answered Sep 21 '22 07:09

Paulo Mattos


If you are using pushviewcontroller method then to dismiss you have to use popviewcontroller method.

Try this:

self.navigationController?.popViewController(animated: true)
like image 40
Hiren Patel Avatar answered Sep 25 '22 07:09

Hiren Patel