繁体   English   中英

弹出 iOS UINavigationController 时如何调用方法?

[英]How to call a method when an iOS UINavigationController has been popped?

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

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

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

您可以创建一个协议,让您在 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)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM