loaded the "FirstView"
这个BUG只发生在,我设置self.window.rootViewController =FirstViewController中,当我的FirstViewController不是rootViewController时,并没有问题。
我在工程中创建了FirstView,由于我的FirstView控件很多,布局很复杂,所以我选择创一个同名的Xib来可视化编程。
文件
代码如下
==========AppDelegate==========
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [self setRootVC];
[self.window makeKeyAndVisible];
return YES;
}
- (UIViewController *)setRootVC{
UIViewController * c = [NSClassFromString(@"FirstViewController") new];
return c;
}
==========FirstViewController==========
@interface FirstViewController ()
@property (nonatomic,weak) FirstView *firstView;
@end
@implementation FirstViewController
-(FirstView *)firstView{
if (!_firstView) {
FirstView * firstView = [FirstView new];
[self.view addSubview:firstView];
_firstView = firstView;
}
return _firstView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.firstView.frame = CGRectMake(0, 30, 100, 100);
self.firstView.backgroundColor =[UIColor lightGrayColor];
}
我还未往FirstView.xib中添加任何控件,也没有加载xib的,然后运行,结果:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "FirstView" nib but the view outlet was not set.'
百思不得其解,应该没有错误啊,然后各种折腾。
再看错误,提示FirstView有问题,于是我创建类secondView来替代FirstView
这回竟然成功了!!!到底是什么情况
······
思来想去,又折腾了一番,仔细再看错误提示:
[UIViewController _loadViewFromNibNamed:bundle:] loaded the "FirstView" nib but the view outlet was not set.'
提示我,加载ViewController时,会触发这个方法_loadViewFromNibNamed:bundle:
这个方法就是用来加载ViewController的View的,所以,由于命名关系,我所创建的FirstView.xib,相当于创建了FirstViewController.xib,所以系统提示我loaded the "FirstView" nib but the view outlet was not set.'
好了,知道原因就好解决了
打开FirstView.xib,指定File's Ower为FirstViewController
然后需要我们手动接连View,并且我为FirstView.xib添加两个label
修改FirstViewController为:
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
@end
再次运行,问题解决。
屏幕快照 2017-11-19 下午4.50.38.png
搞开发将近一年,由于一直都是纯代码Coding,最近才尝试可视化编程。想来这次的坑,我是迟早都会碰到的。其实我一直没搞懂File's ower和View里需要填写的关联类各有什么区别,经多这次的调试,额外的让我明白了File's ower的作用。
以下为File's ower和View里需要填写的关联类的区别:
Xib的使用:设置File's Owner的Class和view的Class的区别