【问题标题】:How to call a method when an iOS UINavigationController has been popped?弹出 iOS UINavigationController 时如何调用方法?
【发布时间】:2020-11-08 21:33:56
【问题描述】:

我有一个 iOS UINavigationController,其 rootViewControllerViewController1ViewController1 通过调用pushViewController(_:animated:) 推送ViewController2,然后ViewController2 通过调用popViewController(animated:) 弹出自身。我没有使用故事板。

现在我已经回到ViewController1,我希望ViewController1 自动执行一些操作。

当我们从ViewController2 返回时,如何安排调用ViewController1 的方法?换句话说,ViewController1 怎么能感觉到它刚刚被发现?我知道我可以通过调用UINavigationControllerDelegatenavigationController(_:didShow:animated:) 方法来做到这一点,但是有更简单的方法吗?

【问题讨论】:

标签: ios swift uinavigationcontroller


【解决方案1】:

您可以创建一个协议,让您在 ViewController2 被解除时通知 ViewController1,有点像这样:

protocol FeedbackDelegate: class {
    func shouldDoSomething()
}

class ViewController1: UIViewController, FeedbackDelegate {
    func thisIsWhereYouPresentVC2() {
        let vc = ViewController2()
        vc.feedbackDelegate = self
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    // MARK: - FeedbackDelegate
    func shouldDoSomething() {
    }
}

class ViewController2: UIViewController {
    weak var feedbackDelegate: FeedbackDelegate?
    
    func thisIsWhereYouDismissVC2() {
        self.feedbackDelegate?.shouldDoSomething()
        self.navigationController?.popViewController(animated: true)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    • 2017-02-08
    • 2012-11-26
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多