ios 尝试呈现模式视图控制器时 UIViewControllerHierarchyInconsistency
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12434937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIViewControllerHierarchyInconsistency when trying to present a modal view controller
提问by Sam J.
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?
这是一个我几个月没碰过的旧项目,想知道什么会导致这样的错误?
回答by Lukasz
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:
这在最新的 Xcode 版本中已经发生在我身上两次。在这两种情况下,我都需要对 UIViewController 的 XIB 文件进行更改(如果是 MapViewController.xib:
BEFORE:
前:
- Move main View out ofView Controller's children:
- RemoveView Controller from the XIB (it is not necessary since File's Ownershould be of its Class already):
- 将主视图移出视图控制器的子视图:
- 从 XIB 中删除视图控制器(这是没有必要的,因为文件的所有者应该已经是它的类了):
AFTER:
后:
回答by JulianSymes
I had this problem when running Apple's example audio app MixerHost on the iOS 6 simulator.
我在 iOS 6 模拟器上运行 Apple 的示例音频应用程序 MixerHost 时遇到了这个问题。
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).
相当于上述修复,即通过将 View 对象拖到顶层来编辑提供的 MixerHostViewController.xib,并丢弃曾经包含它的现在为空的 ViewController,效果很好(虽然不是在我花了几个小时工作之前)找出潜在的问题是什么,所以我现在感觉苹果已经完成了 - 似乎他们收紧了一些东西,但没有费心去检查它是否破坏了他们的示例应用程序)。
回答by Bryan
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.
当我的 NibUIViewController
在顶级文件中有一个时,我遇到了这个问题。所以从 Nib 加载创建了那个UIViewController
,然后我尝试从我的类中使用它,它MapViewController
在你的代码中的位置。
In my case the solution was simply to remove the UIViewController
from my Nib file.
在我的情况下,解决方案只是UIViewController
从我的 Nib 文件中删除。
回答by clearlight
Maybe in some cases it is better to take another approach and notdelete 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.
也许在某些情况下,最好采用另一种方法而不是从 NIB 中删除 UIViewController,因为一方面,通过从 NIB 的层次结构中删除视图控制器,您将失去自动布局边距。
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.
为什么不把 UIViewController 留在你的笔尖 (.xib) 中,并在文件所有者类中为它创建一个出口?然后,不是直接在代码中实例化视图控制器,而是使用 UINIb 类加载笔尖,并在最佳时间(从内存/资源使用的角度来看),调用笔尖实例的 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)
回答by Paresh Navadiya
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];