为了说明加载的过程,我们通过解析UIViewController
实例化到呈现给我们能看到视图这中间的过程
UIViewController
也可以没有xib和StoryBoard进行实例化,这里我们分析的是带有xib或StoryBoard的情况
不管xib还是Storyboard苹果都给给我们提供了上层api用于我们视图和控制器的实例化,分别对应的类是
UINib
和UIStoryboard
。
我们使用的是最上层的api。
我们带有xib的UIViewController
实例化的api如下
[[ViewController alloc] init];
[[ViewController alloc] initWithNibName: bundle:];
带有Storyboard的实例化api如下:
[storyborad instantiateViewControllerWithIdentifier:]
其实接下来苹果会调用UIViewController
的下列方法
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
return [super initWithCoder:aDecoder];
}
打断点可以看到aDecoder
这个不为空,这个对象就是UINibDecoder
的实例,这个类继承于NSCoder
。通过这个类我们可以应该猜出UINibDecoder
给我们做了什么。
其实说白了就是帮我们反序列化了。上边的方法其实仅仅帮我们反序列化了