Use 1 for iOS-xib and 1 for iOS-xib
1. parsing and usage of File's owner:
1.
Storyboard: Describes the software interface. It is generated after iOS5.0.
Xib: Describes the software interface. It is the predecessor of storyboard.
2.
All resources in the project environment must be accessed through [NSBundle mainBundle], such as accessing the full path of images;
=== There is a method: loadNibNamed :( NSString *) owner :( id) option :( NSDictionary *) ===
3. If a control in xib wants to connect to the Controller:
1> the file's owner of xib must set the class as the corresponding controller;
2> loadNibNamed :( NSString *) owner :( id) option :( NSDictionary *) the owner must be set to the corresponding controller (because this parameter is not passed, the file's owner is still blank during running );
3> xib sets the class of the file's owner. Only then can the control in the xib find the corresponding method to call in the file's owner (class or object, such as the Dog object.
4. The essence of xib is xml. The parsing process is as follows:
Certificate ------------------------------------------------------------------------------------------------------------------------------------
5. File's owner steps:
Note: type matching means that the types set in the class of the File's owner must be the same as those of the owner parameter in loadNibNamed :( NSString *) owner option :( NSDictionary.
Ii. view encapsulation (custom view)
The Controller should not be used to listen for button clicks in the xib, because the xib is bound to the Controller, and the coupling is too strong, which is not conducive to expansion and reuse.
Certificate --------------------------------------------------------------------------------------------------------------------------------------------------------------
For ease of viewing, briefly describe the overview of custom views through xib:
======= RowView. m and RowView. h file content and xib file ======
# Import "RowView. h"
@ Implementation RowView
+ (Id) rowViewWithIcon :( NSString *) icon name :( NSString *) name
{
RowView * view = [[NSBundle mainBundle] loadNibNamed: @ "RowView" owner: nil options: nil] [0];
# If 1 // use a tag to display the control in xib
// 1. Set the icon
UIButton * iconBtn = (UIButton *) [view viewWithTag: 1];
[View. iconBtn setImage: [UIImage imageNamed: icon] forState: UIControlStateNormal]; // note: the class method cannot access member variables! Therefore, _ iconBtn cannot be used.
// 2. Set the name
UILabel * nameLabel = (UILabel *) [view viewWithTag: 2];
View. nameLabel. text = name;
# Else // drag-and-drop connection attributes and xib
// 1. Set the icon
// UIButton * iconBtn = (UIButton *) [view viewWithTag: 1];
[View. iconBtn setImage: [UIImage imageNamed: icon] forState: UIControlStateNormal];
// 2. Set the name
// UILabel * nameLabel = (UILabel *) [view viewWithTag: 2];
View. nameLabel. text = name;
# Endif
Return view;
}
@ End
----------------------------------------
# Import <UIKit/UIKit. h>
@ Interface RowView: UIView
+ (Id) rowViewWithIcon :( NSString *) icon name :( NSString *) name;
@ Property (nonatomic, weak) IBOutlet UIButton * iconBtn; // if it is not a drag-and-drop, no more
@ Property (nonatomic, weak) IBOutlet UILabel * nameLabel; // if it is not a drag-and-drop, no more
@ End
----------------------------------------
RowView. xib:
Controller usage:
-(Void) viewDidLoad
{
[Super viewDidLoad];
RowView * row = [RowView rowViewWithIcon: @ "017.png" name: @" jake "];
[Self. view addSubview: row];
}