A small research on the automatic call method of the control system is created. The control system automatically
It hurts to see what the underlying system has done for us whenever we create a control in various ways! I believe that you will often intercept some methods automatically called by the system during development, and then add the code of a function you want to implement in these methods. The following describes some methods that the system automatically calls when creating some controls.
You can create controls in three ways.
1. Pure code
2. storyboard
3. xib
The system automatically calls some underlying methods when creating controls. The common methods are as follows:
Init
InitWithFrame
InitWithStyle
InitWithCoder
AwakeFromNib
To study how to customize a testView class when a control is created, it inherits from the UIView and obtains all the methods and attributes of the UIView, block init, initWithFrame, initWithCoder, and awakeFromNib and add the code to print the method name. The Code is as follows:
1 #import "testView.h" 2 3 @implementation testView 4 5 -(instancetype)init{ 6 if (self = [super init]) { 7 NSLog(@"%s",__func__); 8 } 9 return self;10 }11 12 -(instancetype)initWithCoder:(NSCoder *)aDecoder{13 if (self = [super initWithCoder:aDecoder]) {14 NSLog(@"%s",__func__);15 }16 return self;17 }18 19 -(instancetype)initWithFrame:(CGRect)frame{20 if (self = [super initWithFrame:frame]) {21 NSLog(@"%s",__func__);22 }23 return self;24 }25 26 -(void)awakeFromNib{27 NSLog(@"%s",__func__);28 }29 30 @end
1. Create a custom testView using code-only in the Controller to observe its underlying calling Method
Code:
TestView * test = [[testView alloc] init];
Console output:
13:49:42. 514 test code [901: 157862]-[testView initWithFrame:]
13:49:42. 514 test code [901: 157862]-[testView init]
Conclusion 1: The initWithFrame method is automatically called when a space is created using the code-only method.
2. xib Creation
Code:
TestView * test = [[[NSBundle mainBundle] loadNibNamed: @ "test" owner: nil options: nil] firstObject];
Console output:
13:59:00. 724 test code [992: 192250]-[testView initWithCoder:]
13:59:00. 724 test code [992: 192250]-[testView awakeFromNib]
Conclusion 2: the system automatically calls the initWithCoder and awakeFromNib methods to create controls using xib.
3. Create UITableViewCell. There is an initWithStyle method. What will the system do when creating UITableViewCell?
You can also define a testCell class to inherit from UITableViewCell.
TestCell code:
1 #import "testCell.h" 2 3 @implementation testCell 4 5 -(instancetype)init{ 6 if (self = [super init]) { 7 NSLog(@"%s",__func__); 8 } 9 return self;10 }11 12 -(instancetype)initWithCoder:(NSCoder *)aDecoder{13 if (self = [super initWithCoder:aDecoder]) {14 NSLog(@"%s",__func__);15 }16 return self;17 }18 19 -(instancetype)initWithFrame:(CGRect)frame{20 if (self = [super initWithFrame:frame]) {21 NSLog(@"%s",__func__);22 }23 return self;24 }25 26 -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{27 if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {28 NSLog(@"%s",__func__);29 }30 return self;31 }32 33 -(void)awakeFromNib{34 NSLog(@"%s",__func__);35 }36 37 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {38 [super setSelected:selected animated:animated];39 40 // Configure the view for the selected state41 }42 43 @end
One by one, let's look at the init Creation
Code:
TestCell * cell = [[testCell alloc] init];
Console:
14:11:31. 180 test code [1030: 236258]-[testCell initWithStyle: reuseIdentifier:]
14:11:31. 181 test code [1030: 236258]-[testCell initWithFrame:]
14:11:31. 181 test code [1030: 236258]-[testCell init]
Conclusion 3: When a tableViewCell is created, the system automatically calls the initWithStyle: reuseIdentifier method and the initWithFrame method at the underlying layer. The initWithStyle method is called before initWithFrame, logically speaking, we can think this way. First, determine the style and assign a frame value to it.
Let's see how to create a custom cell through xib.
Code:
TestCell * cell = [[[NSBundle mainBundle] loadNibNamed: @ "testCell" owner: nil options: nil] firstObject];
Console:
15:34:04. 042 test code [1264: 393941]-[testCell initWithCoder:]
15:34:04. 042 test code [1264: 393941]-[testCell awakeFromNib]
Conclusion 4: creating a cell through xib does not call the initWithStyle: reuseIdentifier method, because the cell style has been defined in xib, Which is logically understandable. InitWithCoder is automatically called before awakeFromNib.
I am so anxious to write a blog, hoping to help readers better understand that creating controls is a method automatically called by the system.