找出用户是否按下了 uinavigationcontroller 中的后退按钮?

发布于 2024-11-09 05:50:07 字数 43 浏览 0 评论 0原文

当视图加载时,我想看看是否是因为用户按下了后退按钮。我怎样才能检查这个?

When a view loads, i want to see if it's because the user pressed the back button. How can i check this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

如痴如狂 2024-11-16 05:50:07

我发现检测 UINavigationController 的后退按钮按下(iOS 5.0 之前)的最佳解决方案是验证当前视图控制器不存在于导航控制器的视图控制器堆栈中。

从逻辑上讲,在 - (void)viewDidDisappear:(BOOL)animated 中检查此条件可能更安全,在调用该方法时,视图控制器很可能已从堆栈中删除。

iOS 5.0 之前的版本:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    if (![[self.navigationController viewControllers] containsObject:self]) {
        // We were removed from the navigation controller's view controller stack
        // thus, we can infer that the back button was pressed
    }
}

iOS 5.0+ 您可以使用 -didMoveToParentViewController:

- (void)didMoveToParentViewController:(UIViewController *)parent
{
    // parent is nil if this view controller was removed
}

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:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    if (![[self.navigationController viewControllers] containsObject:self]) {
        // We were removed from the navigation controller's view controller stack
        // thus, we can infer that the back button was pressed
    }
}

iOS 5.0+ you can use -didMoveToParentViewController:

- (void)didMoveToParentViewController:(UIViewController *)parent
{
    // parent is nil if this view controller was removed
}
岁月静好 2024-11-16 05:50:07

在你的 viewWillDisappear 方法中检查

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if ([self isMovingFromParentViewController]) {
      //specific stuff for being popped off stack
    }
}

这仅适用于 iOS 5 之后

in your viewWillDisappear method check

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if ([self isMovingFromParentViewController]) {
      //specific stuff for being popped off stack
    }
}

This is only for post iOS 5

血之狂魔 2024-11-16 05:50:07

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.

溺渁∝ 2024-11-16 05:50:07

为了完整起见,混合使用两个最高票数的答案(12) 在 Swift 中:

override func willMoveToParentViewController(parent: UIViewController?) {
    super.willMoveToParentViewController(parent)
    if parent == nil {
        // view controller is popping
    }
}

For the sake of completeness, mix of two most upvoted answers (1, 2) in Swift:

override func willMoveToParentViewController(parent: UIViewController?) {
    super.willMoveToParentViewController(parent)
    if parent == nil {
        // view controller is popping
    }
}
墨离汐 2024-11-16 05:50:07

这是一个略有不同的场景,但我认为该解决方案可能会帮助其他人。

在我的情况下,我在 UIPopoverController 中有一个 UINavigationController。我需要检测用户是否单击了后退按钮,或者单击了弹出框外部。为此,我检查了 viewWillDisappear 中的visibleViewController 属性。如果关闭时视图控制器仍然是visibleViewController,则弹出窗口正在通过其他方式关闭。如果关闭时视图控制器不是visibleViewController,则后退按钮被按下。

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if (self.navigationController.visibleViewController != self) {
        <Do something since we're closing using something else>
    } else {
        <Do something since we're closing because of the back button>
    }
}

我尝试使用扎克的解决方案,但 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.

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if (self.navigationController.visibleViewController != self) {
        <Do something since we're closing using something else>
    } else {
        <Do something since we're closing because of the back button>
    }
}

I tried using zach's solution, but isMovingFromParentViewController returns true for both cases.

I verified this works in iOS 5+

I hope this helps.

奈何桥上唱咆哮 2024-11-16 05:50:07

创建自定义后栏按钮并设置目标,

步骤 1:将这些方法添加到您的类中

- (void)backButtonClicked :(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addBackBarButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 55, 35);
    [button setTitle:@"back" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;
}

步骤 2:在 viewDiDLoad 方法中调用 [self addBackBarButton];

您将在 backButtonClicked< 中获得操作/strong> 方法。您可以按照您想要的方式使用它。

干杯!

Create a custom back bar button and set the target,

Step 1: Add these methods to your class

- (void)backButtonClicked :(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)addBackBarButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 55, 35);
    [button setTitle:@"back" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;
}

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!

羞稚 2024-11-16 05:50:07

如果来到这里并且只想跟踪导航栏中是否单击了后退按钮(弹出导航),您可以使用 UINavigationControllerDelegate 中的以下方法并检查 operation 是否为 pop 识别弹出导航

func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationController.Operation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    if operation == .pop {
        trackBackNavigationEvent(subject: "top-navigation-bar")
    }
}

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 if operation is pop to identify pop navigation

func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationController.Operation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    if operation == .pop {
        trackBackNavigationEvent(subject: "top-navigation-bar")
    }
}
皓月长歌 2024-11-16 05:50:07

要确保您确定这是后退按钮,唯一的方法是创建一个自定义按钮。如果您不知道该怎么做,请查看此

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文