I have a custom container view controller:
When you use
addChildViewController to add the
ContentPortraitVC gets its
parentViewController property set.
b. You then (as per the Apple documentation) have to manually display the
ContentPortraitVC's view. If you follow the documentation you do this by adding it as a child of the
ControllerVC's top level view.
ContentPortraitVC then calls
presentViewController to display
a. This sets its
presentingViewController property (in the debugger this is shown as the
_parentModalViewController ivar -- note the ivar is different from the property), and sets the
presentedModalViewController ivar of the
ContentPortraitVC (who's ivar is
b. Views wise, on iPhone, the
ContentModalDetailVC's view will completely replace the views from
ContainerVC, so only the modal view controller's view will be visible. (on iPad, it layers the new UI over the top, but as a sibling of the
ControllerVC's view, which in turn is the parent of
So now, you transition from
a. IOS does a bit of magic. It knows that the thing you are removing (
ContentPortraitVC) has a
presentedViewController currently active, so it changes its parent. It sets the value to
ContentPortraitVC, takes the child (the
ContentModalDetailVC) and sets its parent to the new view (
ContentLandscapeVC). So now the view controller that presented the modal view is no longer its presenting view controller. It is as if
ContentLandscapeVC presented it in the first place!
b. In terms of views, you follow the Apple docs to change over the view from
ContentLandscapeVC. But you are simply changing the subviews of
ControllerVC's view. On iPhone, the modal view controller is still the only thing being displayed, so making the change doesn't do anything on screen. On iPad, it does (though you probably won't see it, as the modal view is usually full screen).
Now you come to dismiss the modal view. Presumably you do this in
ContentPortraitVC, but it no longer has any reference to the thing it presented. So calling
[self dismissViewController... does nothing, because
ContentPortraitVC is no longer presenting anything, responsibility for that has been passed on to
So that's what happens and why. Here's what to do about it.
You can rewire the delegate manually when you change from
ContentLandscapeVC, so the latter is the one that tries to dismiss the modal controller.
You can have the modal controller dismiss itself with
[self dismissModalControllerAnimated:YES completion:nil]. I'm going to ask and answer another question on why that works (how does IOS know which to dismiss?), if that seems strange.
You can have the
ControllerVC be the one that pops up the modal view and be responsible for removing it.