Use the XIB file (2) -- associate Variables

Source: Internet
Author: User

XIB file Design Principles

1. Always use a delay to load the NIB file. The NIB file is loaded only when these objects in the NIB file are about to be used immediately.

2. Store Duplicate User interface components (such as document windows) in a separate NIB file.

3. For occasionally used Windows and menus, there is a separate NIB file. In this way, the resources will be loaded to the memory when it is actually used.

4. Set the file's owner to the single external outlet of the NIB file.


The XIB file is usually used together with viewcontroller. When creating viewcontroller, you can create the XIB file associated with it, as shown in figure


Then, you can set iboutlet and ibaction in rootviewcontroller, and use the following code.

RootViewController * rvc;rvc= [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];


In addition to the above usage, there is another common use: when defining complex tableviewcell. For example, advancedtableviewcells (Code portal) in the Apple document ).

Its rootviewcontroller has a tableview in rootviewcontroller. XIB, and an individualsubviewsbasedapplicationcell, as shown in figure


Then, through this code, load the cell into the memory and use

self.cellNib = [UINib nibWithNibName:@"IndividualSubviewsBasedApplicationCell" bundle:nil];[self.cellNib instantiateWithOwner:self options:nil];

At this time, the iboutlet variable tmpcell corresponding to rootviewcontroller is a cell of our design.

When loading the individualsubviewsbasedapplicationcell file, we use uinib, which is actually the class for loading nib files. The first method is to use rootviewcontroller to initialize nib files, the only difference is that rootviewcontroller helps us call the uinib method.


The variables associated with the switch variables are

In the individualsubviewsbasedapplicationcell. h file, the individualsubviewsbasedapplicationcell. H is not the file's owner of the XIB file. How can this association be achieved? The key lies in


We can see that it is the tableviewcell type and has changed to individualsubviewsbasedapplicationcell. In this way, you can associate the content on the cell in the XIB file and view the source code of the XIB file.


Next, let's take a look at mainwindow. XIB of the project and find that there is more content on the left side of the file.


In the ios xib file, you can add any Objetive-C objects to the XIB file, which is created and destroyed by XIB (an autorelease object ). In this file, there are two object containers with advancedtableviewcellsappdelegate and uinavigationcontroller, which are often used in previous versions. In this way, the program consists of mainwindow. when XIB is loaded, the advancedtableviewcellsappdelegate object and navigationcontroller object are available. In the new version of xcode, this is no longer required. Windows and appdelegate have been created.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.