3

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.

ap123
  • 916
  • 1
  • 8
  • 22
  • Have you checked `UINavigationControllerDelegate` methods? I am not sure if it has something for this. – adev Jun 12 '19 at 18:17
  • 3
    Why not use `viewDidDisappear` in the view controller class? – rmaddy Jun 12 '19 at 18:47
  • 2
    Maybe create a **weak** var **optional** in your second view controller that store a reference to the previous viewcontroller, then override `viewWillDisappear(_:)` method in the second view controller and in that func pass the data. – Rey Bruno Jun 12 '19 at 19:56
  • 2
    Or just use your back function inside `viewWillDisappear(_:)` method. – Rey Bruno Jun 12 '19 at 19:58
  • 1
    Wow the `viewWillDisappear(_:)` worked! Can't believe I didn't think of that. Thanks! – ap123 Jun 13 '19 at 16:48

1 Answers1

2

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

fphelp
  • 1,544
  • 1
  • 15
  • 34