找出用户是否按下了 uinavigationcontroller 中的后退按钮?
当视图加载时,我想看看是否是因为用户按下了后退按钮。我怎样才能检查这个?
When a view loads, i want to see if it's because the user pressed the back button. How can i check this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我发现检测 UINavigationController 的后退按钮按下(iOS 5.0 之前)的最佳解决方案是验证当前视图控制器不存在于导航控制器的视图控制器堆栈中。
从逻辑上讲,在
- (void)viewDidDisappear:(BOOL)animated
中检查此条件可能更安全,在调用该方法时,视图控制器很可能已从堆栈中删除。iOS 5.0 之前的版本:
iOS 5.0+ 您可以使用 -didMoveToParentViewController:
The best solution I've found to detect a UINavigationController's back button press (pre-iOS 5.0) is by verifying that the current view controller is not present in the in the navigation controller's view controller stack.
It is possibly safer to check this condition in
- (void)viewDidDisappear:(BOOL)animated
as logically, by the time that method is called it would be extremely likely that the view controller was removed from the stack.Pre-iOS 5.0:
iOS 5.0+ you can use -didMoveToParentViewController:
在你的 viewWillDisappear 方法中检查
这仅适用于 iOS 5 之后
in your viewWillDisappear method check
This is only for post iOS 5
UINavigationController 有一个
delegate
属性,用于发出委托回调。请参阅 iOS 参考在这里。该委托没有“按下后退按钮”回调,但它会告诉您何时某些内容将出现在导航堆栈上。当您按回键时,您将从堆栈中“弹出”顶部视图控制器,因此它会告诉您视图即将出现。我认为这就是您正在寻找的回调。
您可以使用一些简单的逻辑来检查视图控制器是否“感兴趣”,然后您可以发送通知等。
UINavigationController has a
delegate
property that issues delegate callbacks. Please see the iOS reference here.The delegate doesn't have a "back button pressed" callback, but instead it tells you when something is going to appear on the navigation stack. When you press back, you are "popping" the top view controller off the stack, so it will tell you that the view is about to appear. I think this is the callback you'd be looking for.
You could have some simple logic to check if it's the view controller that's "interested", and then you could send a notification, et al.
为了完整起见,混合使用两个最高票数的答案(1,2) 在 Swift 中:
For the sake of completeness, mix of two most upvoted answers (1, 2) in Swift:
这是一个略有不同的场景,但我认为该解决方案可能会帮助其他人。
在我的情况下,我在 UIPopoverController 中有一个 UINavigationController。我需要检测用户是否单击了后退按钮,或者单击了弹出框外部。为此,我检查了 viewWillDisappear 中的visibleViewController 属性。如果关闭时视图控制器仍然是visibleViewController,则弹出窗口正在通过其他方式关闭。如果关闭时视图控制器不是visibleViewController,则后退按钮被按下。
我尝试使用扎克的解决方案,但 isMovingFromParentViewController 对于这两种情况都返回 true 。
我验证了这在 iOS 5+ 中有效,
希望这会有所帮助。
This is a slightly different scenario, but I thought the solution might help others out.
In my situation, I had a UINavigationController within a UIPopoverController. I needed to detect whether the user clicked the back button, or clicked outside of the popover. To do this I checked the visibleViewController property in viewWillDisappear. If the view controller is still the visibleViewController when closing, then the popover is being closed by another means. If the view controller is not the visibleViewController when closing, then the back button was pressed.
I tried using zach's solution, but isMovingFromParentViewController returns true for both cases.
I verified this works in iOS 5+
I hope this helps.
创建自定义后栏按钮并设置目标,
步骤 1:将这些方法添加到您的类中
步骤 2:在 viewDiDLoad 方法中调用 [self addBackBarButton];
您将在 backButtonClicked< 中获得操作/strong> 方法。您可以按照您想要的方式使用它。
干杯!
Create a custom back bar button and set the target,
Step 1: Add these methods to your class
Step 2: Call [self addBackBarButton]; in viewDiDLoad method
You will get the action in backButtonClicked method. You can play around with it the way you want.
Cheers!
如果来到这里并且只想跟踪导航栏中是否单击了后退按钮(弹出导航),您可以使用
UINavigationControllerDelegate
中的以下方法并检查operation
是否为pop
识别弹出导航If coming here and just want to track if back button is clicked (pop navigation) in navigation bar, you can use following method from
UINavigationControllerDelegate
and check ifoperation
ispop
to identify pop navigation要确保您确定这是后退按钮,唯一的方法是创建一个自定义按钮。如果您不知道该怎么做,请查看此
The only way to do this so you know for sure that it was the back button is to create a custom button. If you don't know how to do that, check out this tutorial. It won't look exactly like the normal back button, but close. If you need more help, post a comment