Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Function Called when Back is pressed

Tags:

swift

I would like to perform some actions when a user presses the "Back" button on my Navigation Controller. Is there a Swift function that is called when this happens?

enter image description here

like image 443
GED125 Avatar asked Apr 08 '16 17:04

GED125


1 Answers

Try this (copied and pasted from manecosta)

Replacing the button to a custom one as suggested on another answer is possibly not a great idea as you will lose the default behavior and style.

One other option you have is to implement the viewWillDisappear method on the View Controller and check for a property named isMovingFromParentViewController. If that property is true, it means the View Controller is disappearing because it's being removed (popped).

Should look something like:

override func viewWillDisappear(animated : Bool) {
    super.viewWillDisappear(animated)

    if (self.isMovingFromParentViewController()){
        // Your code...
    }
}

Here is the link to the other question

like image 55
Dan Levy Avatar answered Oct 08 '22 18:10

Dan Levy