self.presentingViewController 返回 UITabBarController 而不是使用presentModalViewController 推送它的视图控制器
我有一个基于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 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:
orpresentModalViewController:
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'smodalPresentationStyle
must beUIModalPresentationCurrentContext
. And in that case, itspresentingViewController
can in fact be the view that "pushes the modal view controller".来自苹果的文档:
因此,即使其他视图控制器推送了它,该视图控制器的视图也会被模态视图覆盖。他们沿着层次结构向上走,直到找到一个视图控制器,其视图没有被遮挡,或者将
definesPresentationContext
设置为 YES,或者直到它到达根视图控制器,在您的例子中是 Tab酒吧控制器。我想说的是,呈现视图控制器需要将
definesPresentationContext
设置为 YES 来回答有关如何返回呈现模式视图控制器的问题。From Apple's documentation:
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.