About nib files in iPhone Development

Source: Internet
Author: User


About nib files in iPhone Development


In iPhone development, the description of the NIB file is described in this article. Let's not talk about it. Let's take a look at the details. A simple iPhone project and code, without using interface builder to develop the iPhone program, a subtle difference loadnibnamed: Owner: Options:

If you have used Delphi, you should be clear about the frm file, that is, the file generated by the form that can be designed by dragging your mouse.

When using xcode for development, we also used the same form to design complex pages through simple drag and attribute settings, and then serialize them to the NIB file, when the program is running, load it in.

During form development in xcode, NIB files are deserialized in two forms.

One is to set the NIB file attribute of the View Controller.

Another method is to use the initwithnibname method.

In fact, there is nothing to say, that is, reading the NIB file, loading, deserialization, generating instances, and displaying.

For the first type, there are some strange things in some places that cannot be understood.

Suppose you want to add a view for the main form window. The existing view-based template in xcode is to create a file with nib (assumed as myviewcontroller. the custom subclass of the uiviewcontroller (assuming myviewcontroller. m ).

Set the Class Name of the View Controller instance in mainwindow. XIB to the custom subclass myviewcontroller, and specify the NIB file as the NIB file (myviewcontroller. XIB) You just created ).

However, after testing, you only need to set the class and do not specify the NIB file.

Of course, if you use viewcontroller's superclass uiviewcontroller in delegate to define the property (the purpose of doing so is to enable mainwindow. plug the View Controller in the XIB and plug it into the outlet in the delegate). You can also specify only the NIB file to load it.

But what if I set the class name and NIB file to an unrelated class and NIB file?

The answer is mainly NIB file.

For example, we set the property of delegate to uiviewcontroller.

However, you can specify the class of View Controller in mainwindow. XIB as myviewcontroller1 (the corresponding NIB file is myviewcontroller1.xib)

However, if we set the NIB file of this view controller to myviewcontroller2.xib (the file owner is myviewcontroller2.m), the view in myviewcontroller2.xib is actually loaded.

In the design of mainwindow. in the XIB View Controller, in addition to loading other nib files, you can also directly design the view, that is, no other NIB file is required, directly in the mainwindow. XIB design. When the two are executed simultaneously, the form designed in mainwindow. XIB is used as the main part, that is, the form is not displayed even if other nib files are loaded.

This fast-food design is not required for people with cleanliness or severe self-obsessive-compulsive disorder.

That is, discard all nib files, and all form designs are completed in the code.

Transform the window-based template,

1. Delete the mainwindow. XIB file from the project.

2. Delete the main NIB file base name entry from info. plist.

3. Edit main. M.

Put this Java code

1 int retval = uiapplicationmain (argc, argv, nil, nil );

2 int retval = uiapplicationmain (argc, argv, nil, nil );

Change to Java code

3 int retval = uiapplicationmain (argc, argv, nil, @ "___ projectnameasidentifier ___ appdelegate ");


5 Int retval = uiapplicationmain (argc, argv, nil, @ "___ projectnameasidentifier ___ appdelegate ");

4. Edit ___ projectnameasidentifier ___ appdelegate. m

Java code

6-(void) applicationdidfinishlaunching :( uiapplication *) Application {


8 // override point for customization after application launch

9 [Window makekeyandvisible];



12-(void) applicationdidfinishlaunching :( uiapplication *) Application {


14 // override point for customization after application launch

15 [Window makekeyandvisible];


Change to Java code

17-(void) applicationdidfinishlaunching :( uiapplication *) Application {

18 uiwindow * WD = [[uiwindow alloc] initwithframe: [uiscreen mainscreen] bounds];

19 self. Window = WD;

20 [WD release];

21 [Window makekeyandvisible];




25-(void) applicationdidfinishlaunching :( uiapplication *) Application {

26 uiwindow * WD = [[uiwindow alloc] initwithframe: [uiscreen mainscreen] bounds];

27 self. Window = WD;

28 [WD release];

29 [Window makekeyandvisible];



Of course, if you do not rewrite the template program, you can use the window-based template to generate the framework.

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.