0

is it possible to handle and trigger some func in first controller if second controller is dimissed?

this my code to open second controller from first controller

self.present(UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LockScreen") as UIViewController, animated: true, completion: nil)

and this my code to dismiss controller from second controller to first controller

self.navigationController?.popViewController(animated: true)

self.dismiss(animated: true, completion: nil)
E-Place
  • 393
  • 4
  • 21
  • 1
    There are several ways to do that. E.g: `delegate`, completion inside `dismiss(animate: completion:)`... But why you popViewController then dimisss instead of dismissing the navigation right away? – son May 25 '21 at 02:19
  • Does this answer your question? [Detect when a presented view controller is dismissed](https://stackoverflow.com/questions/32853212/detect-when-a-presented-view-controller-is-dismissed) – Coder-256 May 25 '21 at 02:20
  • @son ooh ok I just dismiss only. any example to handle that? – E-Place May 25 '21 at 02:37
  • check my answer. @E-Place – son May 25 '21 at 03:06

2 Answers2

1

Inside your LockScreen controller, declare a closure to handle when dismissed:

class LockScreen: UIViewController {
    var onDismissHandler: (() -> ())?
    
    func dismissController() {
        self.dismiss(animated: true, completion: onDismissHandler)
    }
}

Then when you're presenting LockScreen:

func onPresent() {
    let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "LockScreen") as LockScreen
    controller.onDismissHandler = { [weak self] in
        // TODO: Do something when dismissed
    }
    self.present(controller, animated: true, completion: nil)
}
son
  • 504
  • 3
  • 6
0

When second viewController is dismissed, trigger some func on dismissing of the current viewController.

class ViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()
}

@IBAction private func buttonClick(sender: UIButton) {
    let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController")
    onDismiss()
    self.present(viewController, animated: false, completion: nil)
}

Trigger some func on dismissing of the current view controller

func onDismiss() {
    self.dismiss(animated: false) {
        print("Dismissing current view controller")
    }
}

}

Appaiah
  • 1
  • 1