36

尝试使用以下代码呈现模态视图控制器

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

不断收到以下错误..

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

这是一个我几个月没有接触过的老项目,不知道是什么导致了这样的错误?

4

5 回答 5

149

在最新的 Xcode 版本中,这已经发生在我身上两次了。在这两种情况下,我都需要对 UIViewController 的 XIB 文件进行更改(在您的情况下,它将是 MapViewController.xib:

前:

在此处输入图像描述

  1. 将主视图移出视图控制器的视图:
  2. 从 XIB 中删除视图控制器(没有必要,因为文件的所有者应该已经属于它的类):

后:

在此处输入图像描述

于 2012-09-16T21:11:20.000 回答
8

在 iOS 6 模拟器上运行 Apple 的示例音频应用程序 MixerHost 时遇到了这个问题。

上述修复的等价物,即通过将 View 对象拖到顶层来编辑提供的 MixerHostViewController.xib,并丢弃曾经包含它的现在空的 ViewController,效果很好(尽管在我花了几个小时工作之前)找出根本问题是什么,所以我现在感觉苹果已经完成了 - 似乎他们收紧了一些东西,但没有费心检查它是否破坏了他们的示例应用程序)。

于 2012-10-03T16:16:56.337 回答
4

当我的 NibUIViewController在顶层文件中有一个时,我遇到了这个问题。所以从 Nib 加载创建了那个UIViewController,然后我尝试从我的类中使用它,它MapViewController在你的代码中的位置。

在我的情况下,解决方案只是UIViewController从我的 Nib 文件中删除。

于 2012-09-15T18:05:15.673 回答
0

也许在某些情况下,最好采用另一种方法而不是从 NIB 中删除 UIViewController,因为一方面,通过从 NIB 的层次结构中删除视图控制器,您会丢失自动布局边距。

为什么不将 UIViewController 留在您的 nib (.xib) 中并在文件所有者类中为它创建一个出口?然后,不是直接在您的代码中实例化视图控制器,而是使用 UINib 类加载 nib,并在最佳时间(从内存/资源使用的角度)调用 nib 实例的 instantiateWithOwner() 方法来取消归档 NIB 并连接nib 对象指向所有者类的出口。

    @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)
于 2014-12-24T18:04:24.880 回答
0

你应该这样做..

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];
于 2012-09-15T06:25:56.063 回答