IOS Xcode (fast) - how to execute code after unwinding segue

I perform a segue from scene 1 to scene 2. Then I return from scene 2 to scene 1. How can I not only transfer data from scene 2 to scene 1, but also find in scene 1 that I returned from scene 2 and execute the code in scene 1?

In Android, I do this with startActivity and onActivityResult.

+4
source share
2 answers

Entering a state Boolis like another answer indicating a very bad one and should be avoided if possible, as this greatly increases the complexity of your application.

: delegate Controller2.

protocol Controller2Delegate {
  func controller2DidReturn()
}

class Controller1: Controller2Delegate {
  func controller2DidReturn() {
    // your code.
  }

  func prepareForSegue(...) {
    // get controller2 instance

    controller2.delegate = self
  }
}

class Controller2 {
  var delegate: Controller2Delegate!

  func done() {
    // dismiss viewcontroller

    delegate.controller2DidReturn()
  }
}

.

+6

:

class SourceViewController: UIViewController {
  var didReturnFromDestinationViewController = false

  @IBAction func returnToSourceViewController(segue: UIStoryboardSegue) {
    didReturnFromDestinationViewController = true
  }

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

    if didReturnFromDestinationViewController == true {
      // reset the value
      didReturnFromDestinationViewController = false

      // do whatever you want
    }
  }
}
+1

Source: https://habr.com/ru/post/1617583/


All Articles