我有 4 个 UIViewController 子类,分别命名为 AViewController、BViewController、CViewController、DViewController。
现在在工具栏中,单击 A,内容视图显示 AViewController 的视图,...等。
我是一个懒人,讨厌写 4 次 "alloc]initwithnibname" 代码,所以我写了下面的代码在代码中创建它们。
- (void)addChildView:(UIViewController *)childViewController className:(NSString *)viewClassName{
if (childViewController != nil) {
// add view
[self.contentView addSubview:childViewController.view];
}else
{
// init.
Class v = NSClassFromString(viewClassName);
UIViewController *childViewControllerNew = nil;
childViewControllerNew = [[v alloc] initWithNibName:viewClassName bundle:nil];
[self.contentView addSubview:childViewController.view];
}}
但这不会创建任何 ViewController,调试时总是返回 nil。
你能告诉我有什么问题吗?我可以通过这种方法创建子类 UIViewController 吗?
提前致谢!