“This is the 27th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”
Xib or storyboard
- In common
- Both describe software interfaces
- Use the Interface Builder tool to edit it
- The essence is to convert to code to create controls
- The difference between
- Xibs are lightweight and are used to describe local UI interfaces
- Storyboards are heavyweight, describing multiple interfaces throughout the software and showing jump relationships between multiple interfaces
Load the xib
Xib files become NIB files when compiled
- The first way to load
NSArray * xibArray = [[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil] ; return xibArray[0]; Copy the code
- Second way of loading
UINib *nib = [UINib nibWithNibName:NSStringFromClass(self) bundle:nil]; NSArray *xibArray = [nib instantiateWithOwner:nil options:nil]; return xibArray[0]; Copy the code
Log print in xibArray
The controller loads the XIB
-
First, you need to do some work on the XIB file, opening the XIB file
-
Click “File’s Owner” and set Class to xxxViewControler
-
Right click on “Files’s Owner”, there’s a default IBOutlet variable view, see if it’s associated, if not pull down the view below and associate it with the view
-
The first method of loading, passing in the specified XIB (such as CustomViewController)
CustomViewController *custom = [[CustomViewController alloc]initWithNibName:@"CustomViewController" bundle:nil]; Copy the code
-
The second load method does not specify a XIB
CustomViewController *custom = [[CustomViewController alloc]initWithNibName:nil bundle:nil]; Copy the code
-
Step 1: Find a xiB with the same name as the controller class and load it (xxViewController.xib)
-
Step 2: Find a xiB with the same name as the Controller class but without the Controller. If there is one, load it (xxview.xib).
-
Step 3: If no suitable XIB is found, create a view (white view, created for the system itself)
-
Xib custom controls versus code custom
This is a custom view, and we use different initialization methods to determine how it is executed
#import "CustomViw.h"
@implementation CustomViw
- (instancetype)init{
self = [super init];
if (self) {
NSLog(@"%s",__func__);
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
NSLog(@"%s",__func__);
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
}
NSLog(@"%s",__func__);
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
NSLog(@"%s",__func__);
}
@end
Copy the code
-
Initialize a custom control with the init method
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CustomViw *customView = [[CustomViw alloc] init]; } @end Copy the code
log:
-
Initialize the custom control by loading the XIB method
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; CustomViw *customView = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([CustomViw class]) owner:nil options:nil] lastObject]; } @end Copy the code
Log (print three times because CustomViw xiB file has three views)
Summary:
- Initializing a custom control with code does not automatically load the XIB; it does initWithFrame and init
- To initialize the custom control by loading the XIB, just execute initWithCoder and awakeFromNib. If you want to modify the xiB content by code, it is generally recommended to put it in the awakeFromNib method
The control encapsulates
Generally encapsulating a control, it is common to write two method initializers in a custom control to make it easier for developers to use, so that the same effect can be achieved either by init or by loading a XIB
#import "CustomViw.h"
@implementation CustomViw
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
[self setup];
}
- (void)setup{
[self setBackgroundColor:[UIColor redColor]];
}
@end
Copy the code