ios - 尝试呈现模态视图 Controller 时的 UIViewControllerHierarchyInconsistency

标签 ios objective-c xcode uiviewcontroller xib

尝试使用以下代码呈现模态视图 Controller

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

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

最佳答案

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

之前:

enter image description here

  1. 将主视图移出 View Controller 的子级:
  2. Remove 从 XIB 中删除 View Controller (没有必要,因为 File's Owner 应该已经属于它的 Class):

之后:

enter image description here

关于ios - 尝试呈现模态视图 Controller 时的 UIViewControllerHierarchyInconsistency,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12434937/

相关文章:

ios - 在 iOS 中创建自定义声音和振动?

objective-c - UIViewController 中的 pragma 指令

ios - 显示模态 Controller 时与呈现 Controller 交互

iphone - 有没有办法捕获或处理 EXC_BAD_ACCESS?

ios - 更新到 Xcode 13,尝试在 iOS 但不是 macOS 上运行 SwiftUI 应用程序时获得唯一的错误代码

c++ - OpenCV Apple Mach-O 链接器错误 - 架构 x86_64 的 undefined symbol

ios - 如何向iOS模拟器发送模拟远程通知?

ios - 音乐播放器在我进入我的应用程序后立即停止

ios - 我如何在 iOS 上使用 CoreGraphics 渲染的 pdf 上反转文本颜色?

ios - Swift - 将 Int 保存到核心数据只能工作一次