Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewControllerHierarchyInconsistency when trying to present a modal view controller

Trying to present a modal view controller with the following code

MapViewController *mapView = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];     mapView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;     [self.navigationController presentModalViewController:mapView animated:YES];     [mapView release]; 

Keep getting the following error..

'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View <UIView: 0x1ed815a0; frame = (0 20; 320 460); autoresize = W+H; layer = <CALayer: 0x1ed81600>> is associated with <UIViewController: 0x1ed835a0>. Clear this association before associating this view with <MapViewController: 0x1dd947c0>.' 

This is an old project that I havent touched in months, wonder what could cause such an error?

like image 221
Sam J. Avatar asked Sep 15 '12 05:09

Sam J.


People also ask

How do I present a view controller from another view controller?

Start a segue from any object that implements an action method, such as a control or gesture recognizer. You may also start segues from table rows and collection view cells. Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present.

What is view controller in Swift?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.


2 Answers

This happened to me already twice in the newest Xcode release. In both cases I needed to make changes to the UIViewController's XIB file (In you case it would be MapViewController.xib:

BEFORE:

enter image description here

  1. Move main View out of View Controller's children:
  2. Remove View Controller from the XIB (it is not necessary since File's Owner should be of its Class already):

AFTER:

enter image description here

like image 115
Lukasz Avatar answered Oct 06 '22 00:10

Lukasz


I had this problem when running Apple's example audio app MixerHost on the iOS 6 simulator.

The equivalent of the above fix, namely to edit the supplied MixerHostViewController.xib by dragging the View object to the top level, and discarding the now-empty ViewController that used to contain it, worked perfectly (though not before I'd spent hours working out what the underlying problem was, so I'm feeling done-over by Apple at the moment - seems they tightened something up but didn't bother to check if it broke their sample apps).

like image 23
JulianSymes Avatar answered Oct 05 '22 23:10

JulianSymes