Dismiss pushed view controller

13,704

Solution 1

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

self.navigationController?.popViewController(animated: true)

Solution 2

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

Try this:

self.navigationController?.popViewController(animated: true)
Share:
13,704
Doto Pototo
Author by

Doto Pototo

Software Developer

Updated on June 01, 2022

Comments

  • Doto Pototo
    Doto Pototo almost 2 years

    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?

  • Doto Pototo
    Doto Pototo about 7 years
    Well now don't I feel silly! Is there a way to pop a view with a completion method, like you can with dismiss?
  • Hiren Patel
    Hiren Patel about 7 years
    Popviewcontroller does not have completion method.
  • Hiren Patel
    Hiren Patel about 7 years
    But you can check viewwilldisappear method