ViewC *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"myController"];
[self.navigationController pushViewController:viewController animated:YES];
上面的这种情形是经常在,向navigationController中push一个ViewController的时候经常使用的。加载的代码:
ViewC *vc = [[ViewC alloc] initWithNibName:
@"ViewC" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:vc animated:YES];
编译器爆出这样的错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "View" nib but the view outlet was not set.'
1, 打开nib文件
2, 点击"File's Owner", 按command+4,设置Class为xxxViewControler
3, 按Control+"Files's Owner", 里面有个默认的IBOutlet变量view, 看一下后面有没有做关联,如果没有就拉到下面的View和视图做个关联
但关联过后会爆出下面的错误:
*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View <UIView: 0x7ff1d1522cd0; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff1d153f150>> is associated with <ViewC: 0x7ff1d154c060>. Clear this association before associating this view with <ViewC: 0x7ff1d1544dc0>.'
原来不需要对xib文件进行修改,是加载方法的问题,直接使用下面的加载方法便可
ViewC *vc = [[NSBundle mainBundle] loadNibNamed:
@"View" owner:nil options:nil ].lastObject;
[self.navigationController pushViewController:vc animated:YES];