Ios addChildViewController是否出现异常错误?

Ios addChildViewController是否出现异常错误?,ios,objective-c,Ios,Objective C,我不熟悉使用这种方法,因此我可能会完全错误地使用这种方法,因此以下是我的代码: @property (nonatomic, weak) ConverterViewController *converterViewController; @property (nonatomic, weak) CalculatorViewController *calculatorViewController; 如果我正确理解了这段代码,那么它们将作为对两个不同ViewController的引用 然后,我的vie

我不熟悉使用这种方法,因此我可能会完全错误地使用这种方法,因此以下是我的代码:

@property (nonatomic, weak) ConverterViewController *converterViewController;
@property (nonatomic, weak) CalculatorViewController *calculatorViewController;
如果我正确理解了这段代码,那么它们将作为对两个不同ViewController的引用

然后,我的viewDidAppear方法中有以下内容:

[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
[self.view addSubview:_converterViewController.view];
当我尝试将其添加为子视图控制器时,第一行出现了一个NSException。因此,不知道这是否应该调用我的ConverterViewController类中的一些方法,我在该类中设置了一些断点,包括initWithNibName和viewDidLoad方法,我发现这两个方法都没有被调用,所以我不确定到底出了什么问题。我也不确定会出什么问题,所以非常感谢您的帮助

这是我从控制台得到的全部信息:

libc++abi.dylib: terminating with uncaught exception of type NSException

更新答案:

[self addChildViewController:\u converterViewController]不创建
converterViewController

它只需将
converterViewController
对象作为
childViewController
添加到
self

您需要在
-addChildViewController:
之前分配内存并实例化对象
converterViewController
,否则它的值将为
nil
,不会发生任何事情

所以。。。这件事:

_converterViewController = [[ConverterViewController alloc] initWithNibName:@"ConverterViewController"
                                                                     bundle:nil];

//now... adding it as childViewController should work
[self addChildViewController:_converterViewController];
[_converterViewController didMoveToParentViewController:self];
//optional: give it a frame explicitly so you may arrange more childViewControllers
//[_converterViewController.view setFrame:CGRectMake(0,0,100,100)];
[self.view addSubview:_converterViewController.view];

异常名称和原因是什么?这些通常打印到控制台,但您可以在调试器中
po
它们。@BergQuester刚刚运行了调试器,发现它说converterViewController为nil,如何使它包含我的视图控制器?我设置了一个视图控制器来使用这个类。这个错误非常模糊。无论如何。。。尝试
@property(非原子,强)
而不是
@property(非原子,弱)
我同意,尝试
而不是
。如果这不起作用,请向我们展示更多代码,包括填充
calculatorViewController
converterViewController
的位置。发布更多与
converterViewController
相关的代码。包括alloc/init部分(你正在这么做……对吗?)嗯,奇怪的是,我以为我已经解决了这个问题,但是错误发生在这一点之前,所以这并不能解决我当前的问题。我将修改代码以反映它应该是什么。