Life cycle function of UIViewController and UIView under different loading modes

1 basic instructions

Said to do a few years of iOS, UIViewController and UIView for the life cycle function has not been very seriously, resulting in a lot of fuzzy place. Today to write a special Demo to verify it, found that the different loading method is still quite big difference. Specific Demo address UIViewController and UIView lifecycle loading and unloading.

2 loading UIViewController

Each loading method calls the loading method is different, and there are some different details. Apple provides loading four default for us, but I am pure code AutoLayout, we come to the party, the third party layout layout by Masonry tool, in my Git also have different layout solutions specifically summarized. Next, we look at the system in four ways, there is no hesitation in the theoretical nature of things, I directly on the main code:

  • Loading by XIB.
  • Loading by StoryBoard.
  • Loading by NSCoding protocol.
  • Load by code.

2.1 load with XIB UIViewController

First look at the initialization code:

- (IBAction) loadControllerFromXIB: (ID) sender XIBViewController [[XIBViewController alloc]initWithNibName:@ {*xibVC = "XIBViewController" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:xibVC animated:YES];}

Print results when we don’t implement loadView:

2017-04-15 12:05:32.974 UIViewController and UIView life cycle of loading and unloading [59883:1192231] initWithNibName 2017-04-15 12:05:32.987 UIViewController and the UIView life cycle of loading and unloading [59883:1192231] viewDidLoad 2017-04-15 12:05:32.987 UIViewController and the UIView life cycle of loading and unloading [59883:1192231] viewWillAppear 2017-04-15 12:05:32.996 UIViewController and the UIView life cycle of loading and unloading [59883:1192231] viewWillLayoutSubviews 2017-04-15 12:05:32.997 UIViewController and the UIView life cycle of loading and unloading [59883:1192231] viewDidLayoutSubviews 2017-04-15 and 12:05:33.002 UIViewController the life cycle of UIView [59883:1192231] viewWillLayoutSubviews 2017-04-15 12:05:33.002 loading and unloading of the UIViewController and the UIView life cycle of loading and unloading [59883:119 2231] viewDidLayoutSubviews 2017-04-15 12:05:33.506 UIViewController and the UIView life cycle of loading and unloading [59883:1192231] viewDidAppear 2017-04-15 12:05:37.142 UIViewController and the UIView life cycle of loading and unloading [59883:1192231] clickButton //pop 2017-04-15 12:05:42.334 UIViewController and the UIView life cycle of loading and unloading [59883:1192231] viewWillDisappear 2017-04-15 12:05:42.837 UIViewController and the UIView life cycle of loading and unloading [59883:1192231] viewDidDisappear 2017-04-15 12:05:42.838 UIViewController and UIView [59883:1192231] dealloc the life cycle of loading and unloading

I found a strange phenomenon. If the controller is loaded with XIB, and implements an empty loadView, then the view that we have set in the XIB is invalid, and the system should return a default view. However, with the Storyboard load view, to achieve an empty loadView will not be lost inside the Storyboard view, this is a point of attention with XIB and Storyboard.

- (void) loadView{[super loadView]; NSLog (@ loadView);}

2.2 load with Storyboard UIViewController

Initialization code:

- (IBAction) laodControllerFromSB: (ID) sender UIStoryboard [UIStoryboard storyboardWithName:@ {*sb = "Second" bundle:[NSBundle mainBundle]]; SBViewController *sbVC = [sb instantiateViewControllerWithIdentifier:@ SBViewController [self.navigationController pushViewController:sbVC]; animated:YES];}

Operation result:

2017-04-15 12:26:45.364 UIViewController and UIView life cycle of loading and unloading [59932:1194239] initWithCoder 2017-04-15 12:26:45.365 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] awakeFromNib 2017-04-15 12:26:45.368 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] loadView 2017-04-15 12:26:45.368 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidLoad 2017-04-15 12:26:45.368 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewWillAppear 2017-04-15 and 12:26:45.372 UIViewController the life cycle of UIView [59932:1194239] viewWillLayoutSubviews 2017-04-15 12:26:45.373 loading and unloading of the UIViewController and the UIView life cycle of loading and unloading of [59932:1194239] viewDidLayoutSubvie WS 2017-04-15 12:26:45.877 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidAppear //pop 2017-04-15 12:26:50.669 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewWillDisappear 2017-04-15 12:26:51.172 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidDisappear 2017-04-15 12:26:51.172 UIViewController and UIView [59932:1194239] dealloc the life cycle of loading and unloading

For Storyboard, the use of an empty loadView has no effect.

2.3 load with NSCodeing UIViewController

Initialization code:

I am not here / / NSCoding protocol, directly into a nil parameter. - (IBAction) loadControllerFromCoder: (ID) {CoderViewController *coderVC = [[CoderViewController sender alloc]initWithCoder:nil]; [self.navigationController pushViewController:coderVC animated:YES];}

Operation result:

2017-04-15 12:30:25.962 UIViewController and UIView life cycle of loading and unloading [59932:1194239] initWithCoder 2017-04-15 12:30:25.963 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] loadView 2017-04-15 12:30:25.963 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidLoad 2017-04-15 12:30:25.963 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewWillAppear 2017-04-15 12:30:25.967 UIViewController and the UIView life cycle of loading and unloading [59932: 1194239] viewWillLayoutSubviews 2017-04-15 12:30:25.967 UIViewController and the life cycle of UIView [59932:1194239] viewDidLayoutSubviews 2017-04-15 12:30:25.968 loading and unloading of the UIViewController and the UIView life cycle of loading and unloading of [59932:1194239] viewWillLa YoutSubviews 2017-04-15 12:30:25.968 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidLayoutSubviews 2017-04-15 12:30:26.470 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidAppear //pop 2017-04-15 12:30:28.034 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewWillDisappear 2017-04-15 12:30:28.537 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidDisappear 2017-04-15 12:30:28.537 UIViewController and UIView [59932:1194239] dealloc the life cycle of loading and unloading

2.4 load UIViewController with code

Initialization code:

- (IBAction) loadControllerWithNone: (ID) sender {*codeVC = [[CodeViewController alloc]init]; [self.navigationController pushViewController:codeVC animated:YES];}

Operation result:

2017-04-15 12:31:48.785 UIViewController and UIView life cycle of loading and unloading [59932:1194239] initWithNibName 2017-04-15 12:31:48.786 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] init 2017-04-15 12:31:48.787 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] loadView 2017-04-15 12:31:48.787 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidLoad 2017-04-15 12:31:48.788 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewWillAppear 2017-04-15 and 12:31:48.792 UIViewController the life cycle of UIView [59932:1194239] viewWillLayoutSubviews 2017-04-15 12:31:48.792 loading and unloading of the UIViewController and the UIView life cycle of loading and unloading of [59932:1194239] viewDidLayoutSubviews 201 7-04-15 12:31:48.792 UIViewController and UIView life cycle of loading and unloading [59932:1194239] viewWillLayoutSubviews 2017-04-15 12:31:48.792 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidLayoutSubviews 2017-04-15 12:31:49.293 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewDidAppear //pop 2017-04-15 12:31:55.594 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] viewWillDisappear 2017-04-15 12:31:56.098 UIViewController UIView and the life cycle of loading and unloading of [59932:1194239] viewDidDisappear 2017-04-15 12:31:56.098 UIViewController and UIView [59932:1194239] dealloc the life cycle of loading and unloading

3 loading UIView

3.1 load with XIB UIView

Initialization code:

- (IBAction) loadViewFromXib: (ID) sender {XibView = [[[NSBundle mainBundle]loadNibNamed:@ "XIBView" options:nil] lastObject]; [self.view addSubview:xibView];} (owner:self);}

Operation result:

2017-04-15 12:33:22.194 UIViewController and UIView life cycle of loading and unloading [59932:1194239] initWithCoder 2017-04-15 12:33:22.195 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] awakeFromNib 2017-04-15 12:33:22.195 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] willMoveToWindow 2017-04-15 12:33:22.195 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] willMoveToSuperview 2017-04-15 12:33:22.196 UIViewController UIView and the life cycle of loading and unloading [59932:1194239] didMoveToWindow 2017-04-15 12:33:22.196 UIViewController and the life cycle of UIView [59932:1194239] didMoveToSuperview 2017-04-15 12:33:22.197 loading and unloading of the UIViewController and the UIView life cycle of loading and unloading of [59932:1194239] layout Here is the Subviews / UIViewController and click on the 2017-04-15 12:33:25.769 UIView after the removal of the life cycle of loading and unloading of [59932:1194239] willMoveToSuperview 2017-04-15 12:33:25.770 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] willMoveToWindow 2017-04-15 12:33:25.771 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] didMoveToWindow 2017-04-15 12:33:25.771 UIViewController and the UIView life cycle of loading and unloading [59932:1194239] didMoveToSuperview 2017-04-15 12:33: 25.771 UIViewController and UIView life cycle loading uninstall [59932:1194239] and dealloc

3.2 load UIView with code

Initialization code:

- (IBAction) loadViewWithNone: (ID) sender CodeView [[CodeView alloc]init] {*codeView = codeView.backgroundColor; [UIColor = greenColor]; codeView.frame = CGRectMake (0, 500, 100, 50); [self.view addSubview:codeView];}

Operation result:

2017-04-15 12:38:57.562 UIViewController and UIView life cycle of loading and unloading [60323:1208772] initWithFrame 2017-04-15 12:38:57.562 UIViewController and the UIView life cycle of loading and unloading [60323:1208772] init 2017-04-15 12:38:57.562 UIViewController UIView and the life cycle of loading and unloading of [60323:1208772] willMoveToWindow 2017-04-15 12:38:57.563 UIViewController and the UIView life cycle of loading and unloading [60323:1208772] willMoveToSuperview 2017-04-15 12:38:57.563 UIViewController and the UIView life cycle of loading and unloading [60323: 1208772] didMoveToWindow 2017-04-15 12:38:57.563 UIViewController UIView and the life cycle of loading and unloading [60323:1208772] didMoveToSuperview 2017-04-15 12:38:57.564 UIViewController and UIView [60323:1208772] layoutSubviews the life cycle of loading and unloading Click on the 2017-04-15 12:39:02.751 / / after removing UIViewController and UIView life cycle of loading and unloading of [60323:1208772] willMoveToSuperview 2017-04-15 12:39:02.752 UIViewController and the UIView life cycle of loading and unloading [60323:1208772] willMoveToWindow 2017-04-15 12: 39:02.752 UIViewController and the UIView life cycle of loading and unloading [60323:1208772] didMoveToWindow 2017-04-15 12:39:02.752 UIViewController and the UIView life cycle of loading and unloading [60323:1208772] didMoveToSuperview 2017-04-15 12:39:02.752 UIViewController UIView and the life cycle of loading and unloading [60323:1208772] click on the 2017-04-15 12:39:02.753 and UIView UIViewController to remove the life cycle of loading and unloading of [60323:1208772] dealloc

4 Summary

UIViewController hook function summary of different loading methods:

  • XIB loading method initWithNibName loadView (Note: if an empty method, the XIB set the property is invalid, it will override the settings in XIB) viewDidLoad
  • Storyboard loading initWithCoder awakeFromNib loadView (implementation is an empty method or not implemented without effect) viewDidLoad
  • NSCoding loading initWithCoder loadView viewDidLoad
  • Code loading initWithNibName init (this is my initiative to call when the initialization, if the introduction of the nil parameter will not call initWithNibName) loadView viewDidLoad

We can see that the code is loaded and XIB loaded exactly the same way, if there is a XIB load XIB, if there is no XIB can add code view.

UIView hook function summary of different loading methods:

  • XIB loading initWithCoder awakeFromNib willMoveToWindow
  • Code loading method initWithFrame (set frame. Init (init method call) willMoveToWindow

We found that if the init initialization, and then manually set the Frame. Will result in the above call order.