当 uiviewcontroller 从导航堆栈中推送/弹出时如何收到警报
当我的视图控制器从导航堆栈中推送或弹出时,我需要执行某些操作,但不想使用 viewillappear / viewdidappear 或 viewwilldisappear / viewdiddisappear ,因为除了视图控制器被推送/弹出时之外,还有这些覆盖情况。使用navigationcontroller委托和navigationController:didShowViewController:animated:和navigationController:willShowViewController:animated:是正确的方法吗?如果没有,最好的方法是什么?
I need to do certain things when my view controller is both pushed or popped from the navigation stack, but don't want to use viewillappear / viewdidappear or viewwilldisappear / viewdiddisappear since those cover cases besides when the view controller is pushed / popped. Is the correct way to go about this to use the navigationcontroller delegate and the navigationController:didShowViewController:animated: and navigationController:willShowViewController:animated: ? If not, how is the best way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
要查明它何时被推送,您可以使用
UINavigationControllerDelegate
并实现
每当视图控制器被推入导航堆栈时以及每当其顶部的视图控制器弹出时,该方法都会触发,从而显示它再次。因此,您必须使用一个标志来确定它是否已初始化,如果尚未初始化则意味着它刚刚被推送。
要了解它何时被弹出,请使用以下答案:
viewWillDisappear:确定视图控制器是否正在弹出或正在显示子视图控制器
To find out when it's pushed, you can use the
UINavigationControllerDelegate
and implement
This method will fire whenever the viewcontroller is pushed into the navigation stack, and whenever the viewcontroller on top of it is popped off, thus revealing it again. So you have to use a flag to figure out if it's been initialized yet, if it hasn't means it just was pushed.
To find out when it's been popped, use this answer:
viewWillDisappear: Determine whether view controller is being popped or is showing a sub-view controller
您可以尝试当对象从导航控制器堆栈推送或弹出时调用 UINavigationController 委托方法。
You can try UINavigationController delegate methods it calls when object push or pop from navigation controller stack.
下面是一个示例,用于检测视图控制器何时通过覆盖
-viewWillAppear:
推入导航堆栈并通过覆盖-viewWillDisappear:
弹出Here's an example to detect when a view controller is being pushed onto the navigation stack by overriding
-viewWillAppear:
and popped by overriding-viewWillDisappear:
您始终可以创建 UINavigationController 的简单子类并包装其超类的方法,以便在调用它们之前设置一个标志:
ActionNavigationController.h
ActionNavigationController.m
As
pushing
将评估为NO
event 如果没有发生任何事情,则预计将从 UINavigationControllerDelegate 访问此代码。You can always create a simple subclass of the UINavigationController and wrap its superclass's methods so that you set a flag before calling them:
ActionNavigationController.h
ActionNavigationController.m
As
pushing
will evaluate toNO
event if nothing is happening, this code is expected to be accessed from the UINavigationControllerDelegate.请小心使用
如果用户从边缘滑动到右侧以弹出视图控制器(而不是实际弹出它),它将调用上面委托的函数,但不会调用下面的函数
请参阅 https://gist.github.com/nonamelive/9334458
Be careful to use
If user swipe from edge into right for popping view controller(and not actually pop it), it will invoke above delegate's function but not below function
Please refer to https://gist.github.com/nonamelive/9334458
你可以在 willShowViewController 中做类似的事情
You can do something like that in willShowViewController
willShowViewController:(UIViewController *)viewController
动画:(BOOL)动画;
didShowViewController:(UIViewController *)viewController
动画:(BOOL)动画;
这些委托函数不会在 iOS13 设备(使用 Xcode 11 编译)上调用。目前这是一个错误吗?
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated;
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated;
these delegate functions arent invoked on iOS13 devices (compiled with Xcode 11). Is this a bug for now?
将委托设置为导航控制器层次结构内的某个内容是不好的做法,而是观察通知:
UINavigationControllerWillShowViewControllerNotification
UINavigationControllerDidShowViewControllerNotification
并且 userInfo 对象包含“NextVisible”和“LastVisible”视图控制器。然而,在外观方法中使用 isMoving 可能是最好的方法。
It's bad practice to set the delegate to something inside the navigation controller's hierarchy, instead observe the notifications:
UINavigationControllerWillShowViewControllerNotification
UINavigationControllerDidShowViewControllerNotification
And the userInfo object contains the "NextVisible" and "LastVisible" view controllers. However using
isMoving
in appearance methods is probably the best way.