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

前端 未结 2 1083
情深已故
情深已故 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".

提交回复
热议问题