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

前端 未结 2 1070
情深已故
情深已故 2021-02-13 17:08

I have a tab based application. One of the views in it is pushing a modal view controller. If I call self.presentingViewController

相关标签:
2条回答
  • 2021-02-13 17:16

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

    0 讨论(0)
  • 2021-02-13 17:35

    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.

    0 讨论(0)
提交回复
热议问题