self.presentingViewController 返回 UITabBarController 而不是使用presentModalViewController 推送它的视图控制器

发布于 2024-12-20 02:12:37 字数 220 浏览 2 评论 0原文

我有一个基于 tab 的应用程序。其中的一个视图正在推送一个模态视图控制器。如果我在模态视图控制器中调用 self.presentingViewController ,它会返回标签栏控制器,而不是视图控制器这是在推动它。

我在这里缺少什么?有没有可靠的方法来返回视图控制器引发模式?

谢谢。

I have a tab based application. One of the views in it is pushing a modal view controller. If I call self.presentingViewController within a modal view controller, it returns tab bar controller, not the view controller which is pushing it.

What am I missing here? Is there a reliable way to return a view controller raising modal?

Thanks.

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

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

发布评论

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

评论(2

迷爱 2024-12-27 02:12:37

这是 iPhone 应用程序吗?如果是这样,那就解释了这种混乱。在 iPhone 上,唯一的呈现样式是 UIModalPresentationFullScreen - 这意味着在 iPhone 上,根视图控制器始终是呈现视图控制器。那么,在标签栏界面中,UITabBarController 的视图是根视图控制器。

您会注意到,在 iPhone 上,模态视图不会取代视图控制器的视图;而是会替换视图控制器的视图。它取代了整个界面,这意味着它取代了标签栏控制器的视图。这是因为标签栏控制器实际上是呈现视图控制器。

我猜测在 iPhone 上您确实不应该将 presentViewController:presentModalViewController: 发送到选项卡栏控制器包含的视图控制器。您应该将其发送到选项卡栏控制器。因此,该消息将被路由到标签栏控制器。

因此,除非您使用的是 iPad,否则 presentingViewController 的值不会发生任何有趣的事情。在 iPad 上,您可以用模态视图替换视图控制器的视图。为此,模态视图的 modalPresentationStyle 必须是 UIModalPresentationCurrentContext。在这种情况下,它的 presentingViewController 实际上可以是“推动模态视图控制器”的视图。

Is this an iPhone app? If so, that explains the confusion. On iPhone, the only presentation style is UIModalPresentationFullScreen - which amounts to saying that on iPhone, the root view controller is always the presenting view controller. Well, in a tab bar interface, the UITabBarController's view is the root view controller.

You'll notice that on iPhone the modal view doesn't replace your view controller's view; it replaces the whole interface, meaning that it replaces the tab bar controller's view. This is because the tab bar controller really is the presenting view controller.

I'm guessing that on iPhone you really should not be sending presentViewController: or presentModalViewController: to a view controller contained by a tab bar controller. You should be sending it to the tab bar controller. The message is therefore being routed to the tab bar controller for you.

So, nothing interesting is going to happen with the value of presentingViewController unless you're on an iPad. On an iPad, you can make the modal view replace the view controller's view. To do so, the modal view's modalPresentationStyle must be UIModalPresentationCurrentContext. And in that case, its presentingViewController can in fact be the view that "pushes the modal view controller".

儭儭莪哋寶赑 2024-12-27 02:12:37

来自苹果的文档:

此属性的默认实现会向上移动视图
层次结构,从该视图控制器开始。第一个视图
控制器发现收到了
PresentViewController:animated:completion: 方法,或者有它的
DefinePresentationContext 属性设置为 YES 将返回
财产的价值。它会一直沿着层次结构向上移动,直到
找到一个要返回的值或者它到达根视图控制器。

因此,即使其他视图控制器推送了它,该视图控制器的视图也会被模态视图覆盖。他们沿着层次结构向上走,直到找到一个视图控制器,其视图没有被遮挡,或者将 definesPresentationContext 设置为 YES,或者直到它到达根视图控制器,在您的例子中是 Tab酒吧控制器。

我想说的是,呈现视图控制器需要将 definesPresentationContext 设置为 YES 来回答有关如何返回呈现模式视图控制器的问题。

From Apple's documentation:

The default implementation of this property walks up the view
hierarchy, starting from this view controller. The first view
controller it finds that received the
presentViewController:animated:completion: method, or that has its
definesPresentationContext property set to YES is returned as the
value of the property. It keeps walking up the hierarchy until it
finds a value to return or it gets to the root view controller.

So, even though the other view controller pushed it, that view controller's view is covered by the modal view. They walk up the hierarchy until it finds a view controller that doesn't have its view obscured, or has definesPresentationContext set to YES, or until it reaches the root view controller, which in your case was the Tab Bar Controller.

I would say that the presenting view controller needs to set definesPresentationContext to YES to answer your question on how to return that view controller that presented the modal one.

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