37

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?

5 Answers 5

149

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

2
  • 1
    I wish i could upvote you more, this solved my problem with using a separate XIB file to represent the view of a custom ViewController that a UIPopupController was using as its contentViewController.
    – mgrandi
    Jan 9, 2013 at 8:14
  • This solution solve my problem as well. However, don't forget to set the view's referencing outlet with its file's owner!
    – UIChris
    Sep 11, 2013 at 20:56
8

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

5
  • Thank you ever so much for documenting this, it helped me with the exact same thing and I'm such a n00b at figuring this out on my own.
    – Nathan
    Dec 22, 2012 at 8:12
  • can you please give me the flow the details about how to resolve this MixerHost issue?
    – sandy
    Jan 22, 2013 at 7:29
  • Sorry @sandy I can't think of any more I can add to the above. It's just one mouse click-and-drag and one mouse select and one delete. Have you tried it? Did you get stuck? Jan 23, 2013 at 19:04
  • @user1681572 i fixed it only need to drag the UIview outside that was under the viewcontroller, working fine.. thanks
    – sandy
    Jan 24, 2013 at 5:20
  • 1
    @sandy you are right, the second step, deleting the empty view controller, is not strictly necessary, but since it's sample code that you may come back to later and look at, it's probably a good idea to clean up by deleting it. Otherwise you might waste time wondering why it's there. Jan 24, 2013 at 13:30
4

I had this problem when my Nib had a UIViewController in the file at top level. So loading from Nib created that UIViewController, then I tried to use it from my class, which was in the position of MapViewController in your code.

In my case the solution was simply to remove the UIViewController from my Nib file.

1
  • This sounds like the likely solution. Creating a view controller both in code and in the .xib file is a common mistake.
    – Caleb
    Sep 15, 2012 at 18:13
0

You should do it like this..

MapViewController *mapView = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];
UINavigationController *navCntrlr = [[UINavigationController alloc] initWithRootViewController:mapView];
mapView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//hide navigation bar if needed
[self.navigationController presentModalViewController:navCntrlr animated:YES];
[mapView release];
2
  • That just chages the error to... 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <UINavigationController: 0x1e59a900>.'
    – Sam J.
    Sep 15, 2012 at 6:43
  • Thanks, but the code from the edited answer takes it back to the same old error.. 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View <UIView: 0x20579930; frame = (0 20; 320 460); autoresize = W+H; layer = <CALayer: 0x20558970>> is associated with <UIViewController: 0x20558c50>. Clear this association before associating this view with <MapViewController: 0x2054f4c0>.'
    – Sam J.
    Sep 15, 2012 at 6:59
0

Maybe in some cases it is better to take another approach and not delete the UIViewController from the NIB, because, for one thing, by removing the view controller from the NIB's hierarchy, you lose the Auto Layout margins.

Why not just leave the UIViewController in your nib (.xib) and create an outlet for it in the file owner class? Then, rather than instantiate the view controller directly in you code, load the nib with the UINib class, and, at the optimal time (from the memory/resource usage standpoint), invoke the nib instance's instantiateWithOwner() method to unarchive NIB and connect the nib objects to the owner class's outlets.

    @IBOutlet var myViewController: myViewController?

    var nib : UINib?
    nib = UINib(nibName: "TheNib", bundle: nil)
    if (nib == nil) {
        println("could not load nib: TheNib.xib")
    }
    nib!.instantiateWithOwner(self, options: nil)
1
  • consider if I want a subclass of myViewController to be init from nib file. Your solution cannot get the subclass, still myViewController.
    – Wingzero
    Apr 7, 2015 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.