self.presentingViewController returns UITabBarController not the view controller pushing it with presentModalViewController

☆樱花仙子☆ 提交于 2019-12-21 03:57:31

问题


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.


回答1:


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".




回答2:


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.



来源:https://stackoverflow.com/questions/8437908/self-presentingviewcontroller-returns-uitabbarcontroller-not-the-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!