Question

I have a problem with a strange crash on iPad 5.0. This crash only occurs on iOS 5.0 and with iPad with a modal ViewController and that ViewControler style set to anything but fullscreen, which works fine.

I included a code example. If you run it and press the Pagesheet button a Window will appear with a UIWebView and a Youtube video. Once the video start, press "fullscreen" to trigger the bugg. (Only iOS 5, it will work in iOS 4.x).

If you press the fullscreen button, the only difference will be that I am using a fullscreen style for the modal UIViewController and the video plays just fine.

Does anyone know a work around, or a better solution?

Terminating app due to uncaught exception "UIViewControllerHierarchyInconsistency" reason: "child view controller: "UIViewController: 0x85d5e00" should have parent view controller:"NewsTableViewController: 0x855f7b0" but requested parent is:"MPInlineVideoViewController: 0x85d3a20"

Source code (Dropbox) http://db.tt/xqMbeYP1

Was it helpful?

Solution

This is not an error on part of the UIWebView.

What happened was that Apple finally became strict as to the way in which it maintains the application's view hierarchy. Prior to iOS 5.x, developers were able to remove a view from one hierarchy and add it to another. The perfect example of this would be say, the application's main view hierarchy; when presenting a modal view controller, this modal view controller has its own view hierarchy which isn't part of the application's view hierarchy; therefore, if the modal view controller had a subview and within that view it had say a movie (which happens to be a view as well) and this movie were to be maximized, the movie's view would be removed from the modal view controller's hierarchy and added to the application's view hierarchy...

application                                              modal view controller
   |                                                              |
   |                                                              |
window                                                          view
   |                                                              |
   |                                                              |
   |----------------                                   ------------------
   |               |                                   |                |
   |               |                                   |                |
subview 1     subview 2                             subview 3         movie

As may be seen, the modal view controller's view hierarchy lies outside of the application's view hierarchy, this was not a problem prior to iOS 5.x because, when the modal view controller was presented and the movie was maximized, what Apple did was the following:

application                                              modal view controller
   |                                                              |
   |                                                              |
window                                                          view
   |                                                              |
   |                                                              |
   |-------------------------                          ------------
   |          |             |                          |
   |          |             |                          |
  movie    subview 1     subview 2                  subview 3

With the movie appearing above all the other views. This no longer is the case in iOS 5.x+, it is an error to do that and you will be presented with the error with which you were, previously, greeted.

In order to work-around this issue, you need to make the modal view controller's view into the application's view hierarchy by not presenting the view controller as a modal view controller and, then, adding the view controller's view to the application's hierarchy as follows:

                                                          application
                                                               |
                                                               |
modal view controller                                        window
          |                                                    |
          |                                                    |
          ---------------------------------------------------------------
          |                                       |                     |
          |                                       |                     |
         view                                  subview 1            subview 2
          |
          |
    -------------------
    |                 |
    |                 |
 subview 3          movie

From this point forward, everyone needs to really think, from the very beginning, how to properly structure view hierarchies and how they will interact with one another, as well as future scalability within those large projects.

This issue was well-documented by Apple during their 2011 WWDC. It is discussed within Session 102.

OTHER TIPS

I had the same problem. The issue is that the modal isn't added as a child of the view controller that is presenting it. Appears to be a UIKit bug. Our workaround was to make a faux-modal that looked identical. Took a few hours but did the trick.

I had an issue crashing the app when youtube goes to full screen.

It turned out that entering full screen mode calls "viewWillDisappear" on the UIWebView's viewcontroller where I had some code to stop the player, clean remove some views from superviews, etc..

I have solved it by moving "viewWillDisappear" code (shutting down youtube) to "didMoveToParentViewController"

- (void)didMoveToParentViewController:(UIViewController *)parent {
    // parent is nil if this view controller was removed
    if (parent == nil) {
        [self closeModal];    // this is moved from viewWillDisappear - it is only called when "Back" button of navigationcontroller's item is clicked.
    }
}

I hope this helps someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top