如何在 UINavigationController 中收到弹出视图的通知?

发布于 2024-10-21 11:23:30 字数 314 浏览 1 评论 0原文

我想在用户到达某个 UIViewController 时按下 UINavigationController 上的后退按钮时执行操作。

不幸的是,UINavigationControllerDelegate 似乎没有任何方法来获取视图弹出的通知。

作为一种解决方法,我现在在 viewDidDisappear 方法中设置了我的操作,只有当 animatedYES 时才会触发该操作。这可行,但有点难看。

我应该如何正确地做到这一点?

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 技术交流群。

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

发布评论

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

评论(5

迷鸟归林 2024-10-28 11:23:30

处理导航视图控制器(以及模式)弹出的最流行方法是为视图控制器实现 viewWillDisappear

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

    if (self.isMovingFromParentViewController || self.isBeingDismissed) {
        // This view controller is being popped or dismissed
    }
}

The most popular way of handling a pop from navigation view controller (as well as from modal) is implementing viewWillDisappear for your view controller.

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

    if (self.isMovingFromParentViewController || self.isBeingDismissed) {
        // This view controller is being popped or dismissed
    }
}
堇年纸鸢 2024-10-28 11:23:30

如果您在堆栈中拥有对控制器的引用(弹出此控制器时将显示该控制器),您可以注册为委托并通过检查它

navigationController:willShowViewController:animated:

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

navigationController:willShowViewController:animated:
尝蛊 2024-10-28 11:23:30

首先,您需要使 UINavigationControllerDelegate 符合导航控制器并实现此方法

 public func navigationController(
    _ navigationController: UINavigationController,
    didShow viewController: UIViewController,
    animated: Bool) {
    guard let dismissedViewController =
            navigationController.transitionCoordinator?
            .viewController(forKey: .from),
          !navigationController.viewControllers
            .contains(dismissedViewController) else {
        return
    }
    performOnDismissed(for: dismissedViewController)
}

在performOnDismissed函数中,您可以检查dismissedViewController,就像

if (dismissedViewController is DesireViewController)

然后一样,触发您的方法。

First you need to conform UINavigationControllerDelegate to navigation controller and implement this method

 public func navigationController(
    _ navigationController: UINavigationController,
    didShow viewController: UIViewController,
    animated: Bool) {
    guard let dismissedViewController =
            navigationController.transitionCoordinator?
            .viewController(forKey: .from),
          !navigationController.viewControllers
            .contains(dismissedViewController) else {
        return
    }
    performOnDismissed(for: dismissedViewController)
}

In performOnDismissed func, you can check dismissedViewController, like

if (dismissedViewController is DesireViewController)

then, fire your method.

慕烟庭风 2024-10-28 11:23:30

您可以在 viewWillDisappear 时调用委托方法,也可以在 viewWillAppear 上为某些 UIViewController 设置逻辑。

You can either call a delegate method when viewWillDisappear or set logic on viewWillAppear for certain UIViewController.

沦落红尘 2024-10-28 11:23:30

您可以观察 UINavigationControllerDelegate 并检查是否会发生转换:

- (void)navigationController:(UINavigationController *)navigationController
          willShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    {
        if([navigationController.viewControllers containsObject:self])
        {
            NSLog(@"push");
        }
        else
        {
            NSLog(@"pop");
        }
    }

you can observe to the UINavigationControllerDelegate and check if transition will happened:

- (void)navigationController:(UINavigationController *)navigationController
          willShowViewController:(UIViewController *)viewController
                        animated:(BOOL)animated
    {
        if([navigationController.viewControllers containsObject:self])
        {
            NSLog(@"push");
        }
        else
        {
            NSLog(@"pop");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文