Search code examples
iosswiftuinavigationcontrollerback

Adding action to a back swipe Swift 5


In my app I have a UINavigationController that puts a navigation bar on all of my ViewControllers and in every ViewController I have a custom back button. In one specific ViewController I need to send some data when going back to the previous ViewController. I was able to do this when the user presses the back button with a custom function, but this function is ignored when user swipes to go back.

I tried using this answer: Adding an action to back swipes swift 4 but calling visibleViewController or topViewController in the navigation controller only gets the view controller that we're going to (the previous ViewController, not the current one). I need the function inside the ViewController we're in before the user swipes back.

How can I call on the back function I made inside my current ViewController to send the data from that ViewController to the ViewController we'll pop to.


Solution

  • Seems like you just need to use viewWillDisappear(_:) and just put the function you made here.