模态关闭后重新加载主视图

问题描述 投票:-1回答:1

在我的Xcode-App中,可以从每个视图打开一个模式。每个'base'视图都有不同的用途,有的在显示表,有的没有。每当模态消失时,如何实现重新加载'base'视图?

由于视图具有如此不同的结构和目的,这似乎特别棘手。我尝试了viewWillAppearviewDidAppearviewDidLoad,但似乎没有一个可行的方法。

swift xcode uiviewcontroller modalviewcontroller
1个回答
0
投票

您可以设置一个委托模式,以便您的模式视图可以通知它何时会消失或确实消失。

首先,您需要为您的代表创建协议:

protocol ModalViewControllerDelegate: class {
    func modalControllerWillDisapear(_ modal: ModalViewController)
}

然后您的模态应该具有一个委托属性(最终将是呈现控制器),并在需要时触发modalControllerWillDisapear方法:

final class ModalViewController: UIViewController {
    weak var delegate: ModalViewControllerDelegate?

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        delegate?.modalControllerWillDisapear(self)
    }
}

并且将要提供模态控制器的所有视图控制器必须符合该协议,并在演示时将其自身指定为模态的代理:

final class SomeViewController: UIViewController {
    private func presentModalController() {
        let modal = ModalViewController()
        modal.delegate = self
        self.present(modal, animated: true)
    }
}

extension SomeViewController: ModalViewControllerDelegate {
    func modalControllerWillDisapear(_ modal: ModalViewController) {
        // This is called when your modal will disappear. You can reload your data.
        print("reload")
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.