同时引用源视图控制器和目标视图控制器

发布于 2024-12-28 12:28:34 字数 978 浏览 4 评论 0原文

我的应用程序中有一系列 UIViewController。大多数都有导航栏,但有些则隐藏它。

问题是,有时当您在带或不带导航栏的视图之间转换到带或不带导航栏的另一个视图时,会出现一个黑框,在转换过程中会替换导航栏。这个问题在这里讨论:在viewWillDisappear期间隐藏UINavigationController的UIToolbar:

这个解决方案是很好,它确实摆脱了黑匣子,但我真的不希望所谓的“柴郡猫”失踪。我已经使用prepareForSegue、ViewWillAppear、viewWillDisappear 等尝试了无数的解决方案。我能做的最好的就是改变黑条出现的场景。

我的意思是,两个导航栏状态之间有四种视图转换组合(隐藏与不隐藏):

  • 隐藏 - 隐藏
  • 隐藏 - 不隐藏
  • 不隐藏 - 隐藏
  • 不隐藏 - 不隐藏

无论我有什么解决方案尝试过,至少其中一种组合会导致我的黑匣子露出丑陋的头。我遇到的问题是,当从导航控制器的视图堆栈中弹出视图时,我无法找到任何可以获取对源视图控制器和目标视图控制器的引用的地方。

如果我可以在同一事件中获得两个引用,我可以简单地确定组合是什么并适当地处理行为,就像我准备继续一样。

现在,我知道“不可能”是一个合理的(甚至是可能的)答案,但我不会单独接受它作为解决方案。如果确实不可能,我想考虑一个合理的替代方案。例如,我可以手动处理所有视图控制器弹出(包括默认的后退按钮),从而可以从导航控制器的堆栈中获取“即将到来的控制器”。

我只是更喜欢使用内置 API 的解决方案,或者至少是我的控制器不必了解自己的导航栏状态的解决方案。

多谢, 帕特里克

I have a series of UIViewControllers throughout my application. Most of them have the navigation bar but some of them hide it.

The problem is that sometimes as you transition between a view with or without navbars to another view with or without navbars there is a black box that replaces the navbar during the transition. This problem was discussed here: Hiding a UINavigationController's UIToolbar during viewWillDisappear:

This solution is fine and it does get rid of the black box, but I really don't want what was described as a "Cheshire Cat" disappearance. I've tried myriad solutions using prepareForSegue, ViewWillAppear, viewWillDisappear, etc. The best I can do is change the scenario in which the black bar shows up.

By this I mean, there are four combinations of view transitions between the two navigation bar states (hidden vs. not-hidden):

  • Hidden - Hidden
  • Hidden - Not Hidden
  • Not Hidden - Hidden
  • Not Hidden - Not Hidden

No matter what solution I've tried, at least one of those combinations results in my black box rearing its ugly head. The problem I have is that I've been unable to find anywhere that I can get a reference to the source view controller and the destination view controller when popping a view off of the navigation controller's view stack.

If I could get both references in the same event, I could simply determine what the combination is and handle the behavior appropriately like I would in prepare for segue.

Now, I know that "it's not possible" is a reasonable (and even a probable) answer, but I won't accept that as a solution alone. If it is indeed not possible, I'd like thoughts on a reasonable alternative. For example, I could handle all view controller popping manually (including the default back button) and thus could get the "upcoming controller" from the navigation controller's stack.

I would just prefer a solution using built in APIs or at least a solution where my controllers didn't have to be aware of their own navigation bar states.

Thanks a lot,
Patrick

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

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

发布评论

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

评论(1

恍梦境° 2025-01-04 12:28:34

我认为 UINavigationControllerDelegate 就是您所追求的。它声明了两个方法:

  • -navigationController:willShowViewController:animated:

  • -navigationController:didShowViewController :animated:

您需要做的就是将自己设置为父导航控制器并实现这些方法以通知传入的视图控制器。

话虽如此,我从来不需要借助它来隐藏和显示导航栏。严格来说,导航栏始终可见的视图控制器永远不会触及导航栏的可见性。当我进入需要隐藏的视图控制器时,该视图控制器负责隐藏并将其设置回消失之前的先前状态。事实证明,遵循这些标准对我来说是可靠的。

I think UINavigationControllerDelegate is what you're after. It declares two methods:

  • -navigationController:willShowViewController:animated:

  • -navigationController:didShowViewController:animated:

All you need to do is set yourself as the delegate of the parent navigation controller and implement these methods to be notified of incoming view controllers.

Having said that, I've never needed to resort to this for hiding and showing navigation bars. Strictly speaking, view controllers where the navigation bar will always be visible never touch the navigation bar's visibility. When I'm moving into a view controller where it needs to hide, that view controller is responsible for hiding and setting it back to its prior state before disappearing. Following these standards has proven reliable for me.

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