首页 > 解决方案 > ios - 如何知道第二个控制器是否被解雇?

问题描述

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

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

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)

标签: iosswift5

解决方案


在你的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)
}

推荐阅读