Swift 4覆盖func viewDidAppear(_ animated:Bool)当我解除viewcontroller时不触发

时间:2017-10-25 10:18:05

标签: ios swift swift4

我有一个 viewControllerA 以编程方式呈现,在storyboard中没有segue,如下所示:

xlFormulas

然后从 viewControllerA 我使用storyboard segue转到 viewControllerB

当我解除 viewControllerB Find时, viewControllerA 中未触发let storyboard = UIStoryboard(name: "IdStoryBoard", bundle: nil) let controller = storyboard.instantiateViewController(withIdentifier: "idcontroller") as! BController self.present(controller, animated: true, completion: nil)

1 个答案:

答案 0 :(得分:2)

如果你想在解雇 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