如何在 UINavigationController 中收到弹出视图的通知?
我想在用户到达某个 UIViewController
时按下 UINavigationController
上的后退按钮时执行操作。
不幸的是,UINavigationControllerDelegate 似乎没有任何方法来获取视图弹出的通知。
作为一种解决方法,我现在在 viewDidDisappear
方法中设置了我的操作,只有当 animated
为 YES
时才会触发该操作。这可行,但有点难看。
我应该如何正确地做到这一点?
I want to perform a action when the user pressed the back button on my UINavigationController
when arrived at a certain UIViewController
.
Unfortunately it looks like UINavigationControllerDelegate
doesn't have any methods to get notified of the popping of views.
As a workaround I now have in the viewDidDisappear
method my action, that only gets fired when animated
is YES
. This works, but it's a bit ugly.
How should I do this properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
处理导航视图控制器(以及模式)弹出的最流行方法是为视图控制器实现
viewWillDisappear
。The most popular way of handling a pop from navigation view controller (as well as from modal) is implementing
viewWillDisappear
for your view controller.如果您在堆栈中拥有对控制器的引用(弹出此控制器时将显示该控制器),您可以注册为委托并通过检查它
If you have a reference to the controller down the stack, the one which will show when this one is popped, you can register as a delegate and check it through
首先,您需要使 UINavigationControllerDelegate 符合导航控制器并实现此方法
在performOnDismissed函数中,您可以检查dismissedViewController,就像
然后一样,触发您的方法。
First you need to conform UINavigationControllerDelegate to navigation controller and implement this method
In performOnDismissed func, you can check dismissedViewController, like
then, fire your method.
您可以在 viewWillDisappear 时调用委托方法,也可以在 viewWillAppear 上为某些 UIViewController 设置逻辑。
You can either call a delegate method when viewWillDisappear or set logic on viewWillAppear for certain UIViewController.
您可以观察 UINavigationControllerDelegate 并检查是否会发生转换:
you can observe to the UINavigationControllerDelegate and check if transition will happened: