The essence of the Xib file is XML, which describes the interface object, each object has a very important property, identity inspector The class attribute in the panel, loading the Xib file is actually the instance of the interface object corresponding to these classes.
Xib File loading process:
1. Load xib files from disk into memory, there are two techniques to load xib files: NSBundle and uinib.
2. Perform unarchive and initialize operations, which are performed primarily by Initwithcoder: (Nscoder *) decoder in Nscoding protocol.
3. Establish connections:outlets and actions. Outlets uses the Iboutlet keyword to establish each outlet using the Setvalue:forkey: method, so outlet notifications are sent for each KVO establishment. Actions use the Ibaction keyword to replace the void return value to establish each action connection by calling the AddTarget:action:forControlEvents: method. Note that building outlets and actions here is sequential, establishing a outlets connection first, and then establishing the actions connection. The establishment of the actions relies on the outlets established before.
4. Call the Awakefromnib method, first call Super's Awakefromnib method, then you can set up some personalized actions, and some cannot be set at design time operation. Note that the awakefromnib message is only sent to the custom Class specified in Interface Builder and will not be sent to placeholder objects such as the files ' s Owner,first responder.
After that, the object's loading is complete and can be done in a variety of operations.
To load a xib file using NSBundle:
[[NSBundle mainbundle]loadnibnamed:< (NSString *) > owner:< (ID) > options:< (nsdictionary *) >];
This is the most common one, LoadNibNamed:owner:options: The method returns a nsarray* that contains the interface object (class) that is contained in the loaded Xib file.
NSBundle each time the Xib file is uploaded from the disk, and uinib is only the first time to upload the Xib file from disk, then the Xib file is cached in memory, each time a new object, directly access the in-memory xib file to perform the 2-4 steps described above, so there will be a significant performance improvement , and the developer documentation also recommends using the uinib loading technique for those reused xib files, when a low memory warning is received, the Xib file is unloaded from within and loaded from the disk when it is accessed again. Here's a look at the definition of uinib:
Ns_class_available_ios (4_0) @interface uinib:nsobject { @private ID storage;} If The bundle parameter is nil, the main bundle was used.//releases resources in response to memory pressure (e.g. memo RY warning), reloading from the bundle when necessary.+ (uinib *) Nibwithnibname: (NSString *) name Bundle: (NSBundle *) bundle ornil;//If The bundle parameter is nil and the main bundle is used.+ (uinib *) Nibwithdata: (NSData *) data bundle: (NSBundle *) bundleornil;//Returns An array containing the top-level objects from the nib.//the owner and options parameters may both Be nil.//If The owner parameter was nil, connections to File ' s owner was not permitted.//Options was identical to the OP tions specified with-[nsbundle loadnibnamed:owner:options:]-(Nsarray *) Instantiatewithowner: (ID) ownerornil options :(nsdictionary *) Optionsornil; @end
The first two methods are clear, loaded in different ways, and the third method is instantiation (creating the object)
Table View instance:
The details are not to be told.
Create a Xib file that is named empty
Note that the class property of the table View cell is Tableviewcell, not the default uitableviewcell,tableviewcell.h as follows:
@interface Tableviewcell:uitableviewcell@property (Weak, nonatomic) Iboutlet UILabel *lb1; @property (weak, nonatomic) I Boutlet UILabel *lb2;-(ibaction) bt: (ID) sender; @end
Three attributes are linked to the Xib file and should be seen.
The following actions are then performed in the Uitableviewdatasource agent:
In header file declaration uinib *nib;//Instantiation self->nib = [uinib nibwithnibname:@ "Empty" bundle:nil];
And then look at Tableview:cellforrowatindexpath: Methods, the method of implementing the standard is as follows:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath { static NSString *cellidentifier = [NSString stringwithformat:@ "Cell"]; UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) { cell = [[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Cellidentifier] autorelease]; } Config the cell return cell;}
This is the way to create a cell using code, see below using uinib:
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ static NSString *identifier = @ "Cell"; Tableviewcell *cell = [TableView dequeuereusablecellwithidentifier:identifier]; if (cell = = nil) { cell = [[nib Instantiatewithowner:nil Options:nil] objectatindex:0]; } Switch (indexpath.section) {case redsection: cell.lb1.text = @ "LB1"; Cell.lb2.text = @ "LB2"; break; Case bluesection: Break ; Default: [[Cell Textlabel] settext:@ "Unknow"]; } return cell;}
As follows:
The above is only a personal understanding, there are errors in the Welcome to correct.
ios– using uinib to load Xib file implementation UITableViewCell