Dismiss pushed view controller
Asked Answered
H

2

16

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?

Hakodate answered 24/3, 2017 at 18:5 Comment(0)
A
48

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

self.navigationController?.popViewController(animated: true)
Anthodium answered 24/3, 2017 at 18:15 Comment(0)
S
10

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

Try this:

self.navigationController?.popViewController(animated: true)
Sissie answered 24/3, 2017 at 18:10 Comment(3)
Well now don't I feel silly! Is there a way to pop a view with a completion method, like you can with dismiss?Hakodate
Popviewcontroller does not have completion method.Sissie
But you can check viewwilldisappear methodSissie

© 2022 - 2024 — McMap. All rights reserved.