Ios 如何将子视图添加为视图控制器视图的子视图?

Ios 如何将子视图添加为视图控制器视图的子视图?,ios,objective-c,addsubview,Ios,Objective C,Addsubview,到目前为止,我已经在主视图控制器中添加了一个子视图。但是,我希望这个子视图是我的视图控制器视图的子视图,所以我尝试这样做 [self.view addSubview:_converterViewController.view]; 但这给了我一个错误: libc++abi.dylib:以类型为的未捕获异常终止 N例外 我知道这很模糊,但出于某种原因,这是我所能得到的。那么我做错了什么?以下是我与此任务相关的代码,它们都位于我的视图控制器ViewDidDisplay方法中 [self addCh

到目前为止,我已经在主视图控制器中添加了一个子视图。但是,我希望这个子视图是我的视图控制器视图的子视图,所以我尝试这样做

[self.view addSubview:_converterViewController.view]; 
但这给了我一个错误:

libc++abi.dylib:以类型为的未捕获异常终止 N例外

我知道这很模糊,但出于某种原因,这是我所能得到的。那么我做错了什么?以下是我与此任务相关的代码,它们都位于我的视图控制器ViewDidDisplay方法中

[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
[self.view addSubview:_converterViewController.view];//this line produces the error
谢谢你的帮助

更新错误:

'NSInternalInconsistencyException', reason: 'Could not load NIB...

以下是添加childViewController时需要执行的步骤

UIViewController *viewController = [UIViewController new];

[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
1-创建子视图控制器
2-添加是作为一个孩子
3-将其视图添加到视图层次结构中 4-通知childViewController它已添加到父级

对于情节提要,请执行以下操作:

//instead of
//[[ConverterViewController alloc] init];
//do
//in my case, Storyboard name was "Main"
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
_converterViewController = [storyboard instantiateViewControllerWithIdentifier:@"ConverterViewController"];

//add as childViewController
[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
//optional: modify parameters if needed
[_converterViewController.view setBackgroundColor:[UIColor redColor]];
[_converterViewController.view setFrame:CGRectMake(0, 64, 320, 100)];
[self.view addSubview:_converterViewController.view];
假设:
  • 故事板的名称是“
    Main
    ”(您的可能只是“故事板”)

  • 情节提要中
    ConverterViewController
    的情节提要标识符是
    ConverterViewController


  • 对于我的情况,在IB中,我避免了将多个视图作为UI设计的同一视图控制器的一部分,但是在解决了设计问题后,我恢复了这种方法。这消除了使用子视图控制器所需的所有代码,并使我对该问题解决方案的需求变得无效。我是在这个人发布他可能的解决方案之前不久这样做的,我不想麻烦地去看看他的解决方案是否有效


    尽管我决定使用其他方法,但我非常感谢在这件事上得到的所有帮助。

    libc++abi.dylib的可能重复:以NSException类型的未捕获异常终止是错误的最后一行。向上滚动控制台一点,您应该会看到类似于
    ***由于未捕获异常而终止应用程序…
    。这是你需要在这里引用的。这解释了很多,我将添加错误。Thanks@JDOdle:您在您的项目中是否看到
    ConverterViewController.xib
    ?@staticVoidMan否,我正在使用Xcode 5。他们是否删除了Xcode 5中的xib文件或移动了这些文件或其他内容?添加子视图控制器没有问题。正如您所说,这是加载nib文件的问题。正如您所说,“无法加载NIB…”查看您的IBOutlet和IBAction是否连接正确