[英]ViewController popped immediately after been pushed in UINavigationController
[英]How to call a method when an iOS UINavigationController has been popped?
我有一个 iOS UINavigationController
,其rootViewController
是ViewController1
。 ViewController1
通过调用pushViewController(_:animated:)
推送ViewController2
,然后ViewController2
通过调用popViewController(animated:)
弹出自身。 我没有使用 storyboard。
现在我已经回到ViewController1
,我希望ViewController1
自动执行一些操作。
当我们从ViewController2
返回时,如何安排调用ViewController1
的方法? 换句话说, ViewController1
怎么能感觉到它刚刚被发现呢? 我知道我可以通过调用UINavigationControllerDelegate
的navigationController(_: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.