Detailed version of iOS controller View loading order

http://www.jianshu.com/p/e1ed4fe1f98c

 

Loading process:

1 In general, the init method is called or the initWithNibName method is called to instantiate the UIViewController, no matter which method is called, the initWithNibName is called (the method is defined as follows)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

2 Then the loadView method will be called to generate UIViewController.view

- (void)loadView

Let's briefly talk about the bottom layer of loadView

2.1.1. Determine if there is a storyboard specified, and if so, load the view of the controller described by the storyboard
2.1.2. Determine whether the nibName is specified, and if so, load the view of the controller described by the nibName

   - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

If judging whether nibName is specified, [self nibName]
2.1.3 judge whether the nibName is empty, if it is empty, he will try to find out if there is a xib with the same name as the controller, but the xib
2.1.4 without the Controller has the same name as the controller The xib, nibName = ViewController, but this step is conditional, the premise is that you have not rewritten loadView
2.1.5 If none are found, directly create the view of the default controller

3 - (void)awakeFromNib;

When this method is used, the outlet has not been connected yet. When the view controller is just built from the storyboard, it is not fully built, but there may be some things to be done in this method, such as splitViewDelegate, which needs to be done very early. Then call viewDidLoad method
4 - (void)viewDidLoad

If the loadView cannot generate UIViewController.view, the system will repeatedly call the loadView and viewDidLoad methods, and finally call the [super loadView] method to return to UIViewController.view
and then call the following two methods in turn. These two methods are also very important. The POP operation in UINavigationController Then sometimes the View in the UIViewController to be displayed is not released (it may also be released), UIViewController will not call the above three methods (initWithNibName, loadView, viewDidLoad) but will call the following two methods
5 - (void)viewWillAppear :(BOOL)animated;

6 - (void)viewDidAppear:(BOOL)animated;
Then call the following two methods for the frame value layout in the view to make the frame value of the child controller more accurate
7 - (void)viewWillLayoutSubviews
8 -(void)viewDidLayoutSubviews

9 Uninstallation process

- (void)viewWillDisappear:(BOOL)animated;
- (void)viewDidDisappear:(BOOL)animated;
- (void)viewWillUnload;//iOS5.0添加
- (void)viewDidUnload;
- (void)dealloc;

The distinction between some methods of controller View

viewDidLoad is actually nothing to be confused. No matter what way to load (Xcode or IB, the loading here belongs to instantiation), this method will definitely be executed after the view.
LoadView needs to be divided into two situations. When you instantiate a class through Xcode You need to implement this method in the controller yourself. You don't need to implement it when instantiating in IB. The
initWithNibName method is created in IB in the controller class, but is used when instantiating the controller through Xcode. The
awakeFromNib method is When a class is instantiated in IB, it is called. After reading the post, I found that everyone recommends using viewDidLoad instead of awakeFromNib, because viewDidLoad will be called multiple times, and awakeFromNib will only be unarchived from the nib file. Called once. The actual test found that when awakeFromNib of a class is called, then the viewDidLoad of this class will not be called, which feels very strange.
initWithCoder is a class created in IB but instanced in Xcode It is called when the nib file is created. For example, create a controller's nib file through IB, and then instantiate the controller through initWithNibName in xocde, then the controller's initWithCoder will be called.
awakeFromNib When the .nib file is loaded, it will be sent An awakeFromNib message goes to each object in the .nib file, and each object can define its own awakeFromNib function to perform some necessary operations in response to this message. That is to say, to create a view object through a nib file is to execute awakeFromNib

viewDidLoad is executed when the view object is loaded into memory, so whether the object is created through a nib file or code, viewDidLoad will be executed



Text / _ That You Meow (author of Jianshu)
Original link: http://www.jianshu.com/p/e1ed4fe1f98c
The copyright belongs to the author, please contact the author for authorization, and mark "author of Jianshu".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326488547&siteId=291194637