Use the XIB file (1) -- parse the XIB File

Source: Internet
Author: User

Using xcode for iOS projects often deals with XIB files. Because XIB files intuitively display the running view, it is very easy to get started and easy to use, however, most kids shoes that have never written views using pure code have some one-sided understanding of XIB.

What is an XIB file?

A nib file describes the visual elements of your application's user interface, including windows, views, controls, and other others. it can also describe non-visual elements, such as the objects in your application that manage
Your windows and views.

The above is the definition on guides. The NIB file (that is, the XIB file) is a visual element that describes the appearance of an application, including windows, views, controls, and others. It can also describe non-visual elements, for example, you can manage the window and view objects in your application.

That is to say, XIB is a description document, which contains elements related to the user interface and user interface. The main reason for this is that it is convenient for programmers because it is "What you see is what you get" and programmers do not need to write a bunch of code as they did before, the execution result of the interface elements can be viewed only after running, which is very inefficient. For more information, see the official website.

How does an XIB file achieve "What you see is what you get "?

In xcode, open the XIB file in the form of "source code" and find that it is an XML-encoded file, that is to say, the essence of the XIB file is to use an XML script to describe the programmer's settings for the view and its subclass in xcode, here we know how the settings in xcode are saved, and how the XIB file is re-executed in the application to the memory.

Create a blank XIB file in xcode and get the following code:


XML is parsed and Graphically displayed. The XIB file is still complex. The preceding items are system and version descriptions, which can be ignored first, the focus is on the content in the blue box (the following explanation is not an official version, please read it carefully ).


We can refer to the name "ibdocument. integratedclassdependencies. This array describes ibdocument-related classes. ibproxyobject (related to file's owner) and ibuiview (related to the main view) are displayed by default ), if we drag a label into the view, the uilabel class will be described here. If we select layout, nslayoutconstraint will be available.



This piece of metadata has metadata, and we don't know how to use it yet. But next we will see the full view.


Like the name of the rootobjects, this array contains all objects in the XIB file. The first is the files owner, and the second is the firstresponder. You can see that all objects use proxyobject. Here is a detail about the implementation of XIB files. When someone in our application reads and uses this user interface, it comes to the viewcontroller in our code, at this time, viewcontroller is in the memory, so how to associate the configurations read from the XIB file with viewcontroller is a problem. Here, proxyobject is used as the proxy object of viewcontroller in the xib file, as shown in


Next, let's look at XML:


In ibclassdescriber, the association between iboutlet and ibaction and XIB is described here. ibobjectcontainer contains the relationship between objects,

After XIB is unarchive, the newly created objects (created through the XIB file) are set as mutual references.

This should be a complex process. Refer to the official document XIB file loading steps as follows:

1. Load the content of the NIB file and Its referenced resource file to the memory:
2 unarchives nib object graph data and instantiate these objects. Initialize each new object based on the object type and Its Encoding in the document.

3. Through the initialization method, all the connections (actions, outlets, and bindings) It reconstructs are objects in the NIB file.

In the above three steps, iOS and OS X are not the same. Here we mainly talk about the object created in iOS, that is, the NIB file. Every time no outlet points to reconnect, it is based on nskeyvaluecoding.
Protocol, which is completed by setvalue: forkey.

The above section mainly describes the XML data and official documents of the XIB file, completes a general understanding of the content in the XIB file, and does not thoroughly analyze its implementation details, the main purpose is to allow us to use the XIB file, which is no longer unfamiliar with it. When an error occurs while executing the XIB file, we can infer the error based on the error message.

NIB files store the objects of your application's user interface





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.