Differences between XIB and nib

Source: Internet
Author: User

Both XIB and nib are graphic interface design documents of interface builder. The name of NiB comes from the nextstep system. Before nextstep was acquired by Apple, NIB was used as the graphic document of interface builder, NIB has developed nib2.0 and nib3.0. After nextstep is acquired by Apple, nib with the nextstep mark is replaced by XIB.

Unlike nib, XIB is a plain text file in XML format, while NiB is a binary file. XIB has obvious advantages over nib, that is, XIB can easily perform diff operations. Because XIB is a text file, it is more advantageous in version control and management than nib. However, no matter whether the nib or XIB format is selected in interface builder, xcode will get a compiled binary NIB file for running the program. Currently, the latest version of xcode uses the XIB format document by default when creating a project.

In fact, the nib generation process is a serialization process,

NIB file generation involves two types of serialization.

1. serialization of the document used by IB. The serialized result is the XML file. It stores the resource information of all interfaces and the relationship between objects.

2. serialization of the XML file during compilation. the serialization result is a binary NIB file.

 

People who have written ibplugin should know that all controls must support the nscoding specifications of initwithcoder and encodewithcoder, which are used for nskeyedarchiver serialization.

The following operations are performed on the XML file during compilation:

1. Read the XML file, generate all interface objects, generate all objects (custom controller classes, etc.), and set the connection between each obejct (ibaction, iboutlet)

2. serialize the object, that is, call the encodewithcoder method to serialize the object, and generate a binary NIB file to generate a binary file, because the class instance generated from the binary file is faster.

The process of deserializing nib files while the program is running

NIB operation during runtime
1. deserialization: Call the initwithcoder method of all objects
2. Some classes such as initwithframe and init are called to generate all class instances.

3. Send an awakefromnib message. awakefromnib of each class instance is called.

 

Referring to the book cocoa programming for Mac OSX, interface builder "freezes" the objects of various controls on the window, menu bar, and window in a nib document; when the program runs, these objects will "wake up ".

It is suggested that the extra binary files in the nib2.0/3.0 era should be compiled and serialized files, that is, the nib files in nib2.0/3.0 are composed of interface information files and runtime binary files.
From the above, we can see that all objects dragged to nib will be automatically created instances, without manual creation of your code (literacy ...), If necessary, add an iboutlet.

 

I have already written an article about the nib loading process, And I Will reference it directly.

Address: http://edsioon.me/nib%E6%96%87%E4%BB%B6%E5%8A%A0%E8%BD%BD%E6%B5%81%E7%A8%8B/

 

A nib file is a resource document that stores the relationships between objects and objects created by interface builder. Objects in the NIB file include: interface objects (visual objects-Windows, views, controls, menus, etc.; non-visual objects-window controllers, view controllers, program delegate proxies, etc) and placeholder object (File's owner, first responder, application ). It describes the object and the relationship between objects. The relationships between objects include: the owner-file's owner connecting to the NIB file, property instance variable binding, Action Method binding, and delegate binding. After the NIB file is loaded, the interface object is created, and the placeholder object is not created. You should create this placeholder object in your code and pass it to the loading code of nib.

1. Load NIB file content and related resources to the memory

  • Load the raw data of all the nib object graphs to the memory, but the data is serialized (Unarchived.
  • Load all custom image resources related to the NIB file to the memory and add them to the cocoa image cache.
  • Load all sound resources related to nib files to the memory and add them to cocoa sound cache.

2. deserialize nib object graph data and initialize the object. How each new object is initialized depends on the object type and serialization method.

  • Normally, the object is initialized through initwithcoder. In iOS, any object that complies with the nscoding protocol is initialized through the initwithcoder method. This includes all the subclasses of uiview and uiviewcontroller, regardless of the default class or custom class of the xcode library.
  • The custom view in OS X is initialized through initwithframe, but this method is not used in IOS to initialize the custom class.
  • Other custom objects are initialized using the init method.

3. re-create all the contact (action and outlet) of the object in nib, including the contact with the file's owner and other placeholder objects.

  • Outlet connections: in IOS, NIB loads code and uses setvalue: forkey: to reconnect to outlet. Setting an outlet in IOS will send a KVO notification to all registered observers. These notifications may be sent before all object contact reconstruction, and it will certainly happen before any awakefromnib of the object is called.
  • Action connections: in iOS, when nib loads code, the addtarget: Action: forcontrolevents of uicontrol is called to set the action. If the target parameter is nil, the action will be processed by the responder chain.

4. Send an awakefromnib message to the corresponding object in nib. In iOS, this message is sent only to the object initialized by the nib Load Code. The file's owner, first responser, and any placeholder object cannot receive the awakefromnib message.

5. display the objects with the visible property "yes.

Summary:
A. Load the NIB file-> Create an object alloc-> initialize init-> create a connection between objects-> send an awakefromnib message to the created object.
B. during initialization of the init method, all iboutlet instance variables are nil, and only all objects (including windows, text edit boxes, buttons, etc.) in the NIB file are created ...), All connections are complete.

C. Once all connections are established (that is, the object address is connected to the instance variable), awakefromnib is sent to all objects. In the awakefromnib method, the instance variables are connected and valid.

 

Reference: http://blog.sina.com.cn/s/blog_6291e42d0102uww2.html

 

 

 

Differences between XIB and nib

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.