Michel
Michel

Reputation: 11749

navigationBar:shouldPop is not functioning as expected

In order to perform some verifications when the back button is pushed on a UINavigationController. I decided, after searching the net on how to do that, to subclass UINavigationController and implement the UINavigationBarDelegate.

Here is my problem: as long as I do not actually implement anything of the UINavigationBarDelegate protocol it all works as when I was using a plain UINavigationController. But when I implement only this function:

    func navigationBar(_ navigationBar: UINavigationBar,
                   shouldPop item: UINavigationItem) -> Bool {
        print(#function)
        return true
    }

I can see in the debugging console that the function is actually called. But the pop does not happen, only the back button vanishes, the view stays there. I expect that with only the function above it should still work as before (i.e. the view should pop out normally).

Anyone can see the thing I am missing?

Upvotes: 0

Views: 605

Answers (2)

Museer Ahamad Ansari
Museer Ahamad Ansari

Reputation: 5523

You are missing to pop view controller add one more line self.popViewController(animated: true) in your code

func navigationBar(_ navigationBar: UINavigationBar,
                       shouldPop item: UINavigationItem) -> Bool {
    print(#function)
    self.popViewController(animated: true)
    return true
}

Upvotes: 1

cwilliamsz
cwilliamsz

Reputation: 718

You can create a back button and add the code below in the action of this button that will call the previous view as if it were the back of navigationbar. Within the action of this button you can add the actions that you want to, if you have any questions retone me.Hide button must be called in viewDidLoad()

// Action    
self.navigationController?.popViewController(animated: true)

// Hide backbutton
self.navigationController?.navigationItem.hidesBackButton = true

Upvotes: 0

Related Questions