ios - 如何知道第二个控制器是否被解雇?

ios - how to know if second controller is dismissed?

如果第二个控制器被关闭,是否可以在第一个控制器中处理和触发一些功能?

这是我的代码,用于从第一个控制器打开第二个控制器

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

这是我的代码,用于将控制器从第二个控制器解除到第一个控制器

self.navigationController?.popViewController(animated: true)

self.dismiss(animated: true, completion: nil)

在您的 LockScreen 控制器中,声明一个闭包以在关闭时处理:

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

那么当你展示时 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)
}

当第二个 viewController 被关闭时,触发一些关闭当前 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)
}

在关闭当前视图控制器时触发一些功能

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

}