我有一个viewControllerA,以编程方式在情节提要中没有顺序出现,就像这样:

let storyboard = UIStoryboard(name: "IdStoryBoard", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "idcontroller") as! BController
self.present(controller, animated: true, completion: nil)

然后,使用情节提要segue从viewControllerA转到viewControllerB。

当我关闭viewControllerB self.dismiss(animated: true, completion: nil)时,在viewControllerA中不会触发viewDidAppear

最佳答案

如果要在关闭viewControllerB时在viewControllerA中触发事件,建议使用创建一个委托方法来链接两者。

在您的viewControllerA上包括以下内容:

protocol TriggerEventDelegate {
    func eventToBeTriggered();
}

然后使您的viewControllerA符合该协议:
extension AController: TriggerEventDelegate {
    func eventToBeTriggered() {
         // Implement whatever you want to trigger here
    }
}

在您的viewControllerB 上,创建一个委托引用:
class BController: UIViewController {
    weak var delegate: TriggerEventDelegate?
}

每当您关闭viewControllerB时,实现触发:
func dismiss() {
    delegate?.eventToBeTriggered()
    self.dismiss(animated: true, completion: nil)
}

最后但并非最不重要的,当从viewControllerA移到viewControllerB时,请使用viewControllerA设置delegate:
func pushFromAToB() {
    let storyboard = UIStoryboard(name: "IdStoryBoard", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier:     "idcontroller") as! BController
    controller.delegate = self
    self.present(controller, animated: true, completion: nil)
}

现在,您的事件将被正确触发,而无需依赖viewDidAppear

关于ios - Swift 4覆盖func viewDidAppear(_动画: bool )我解雇viewcontroller时不触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46929774/

10-11 00:41