[英]How to call a method when an iOS UINavigationController has been popped?
I have an iOS UINavigationController
whose rootViewController
is ViewController1
.我有一个 iOS UINavigationController
,其rootViewController
是ViewController1
。 ViewController1
pushes ViewController2
by calling pushViewController(_:animated:)
, and then ViewController2
pops itself by calling popViewController(animated:)
. ViewController1
通过调用pushViewController(_:animated:)
推送ViewController2
,然后ViewController2
通过调用popViewController(animated:)
弹出自身。 I am not using a storyboard.我没有使用 storyboard。
Now that I have arrived back in ViewController1
, I would like ViewController1
to do something automatically.现在我已经回到ViewController1
,我希望ViewController1
自动执行一些操作。
How can I arrange to have a method of ViewController1
called when we have returned there from ViewController2
?当我们从ViewController2
返回时,如何安排调用ViewController1
的方法? In other words, how can ViewController1
sense that it has just been uncovered?换句话说, ViewController1
怎么能感觉到它刚刚被发现呢? I know I can do this by calling the navigationController(_:didShow:animated:)
method of a UINavigationControllerDelegate
, but is there a simpler way?我知道我可以通过调用UINavigationControllerDelegate
的navigationController(_:didShow:animated:)
方法来做到这一点,但是有更简单的方法吗?
You can create a protocol that lets you notify ViewController1 when ViewController2 is dismissed, somewhat like this:您可以创建一个协议,让您在 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.