IOS program Startup Process Introduction

Source: Internet
Author: User

Everyone will write the helloworld program when learning about iPhone development. Generally, you generate a project through the wizard and then start the application through the simulator. But do you know the startup process behind it? That is, when you click the program icon to start the program and exit the program, what happens to the code under the whole running process? If you understand this, you can easily master the development of the iPhone program. Otherwise, you may feel a little confused when writing the program.

Before introducing the helloworld program, let's take a look at the uiapplication:

Uiapplication

The core function of uiapplication is to provide control and collaborative work during IOS program running.

One of the main tasks of uiapplication is to process user events. It starts a queue, puts all user events in the queue, and processes them one by one. during processing, it sends the current event to a target control suitable for processing the event. In addition, the uiapplication instance maintains a window list (uiwindow instance) opened in this application so that it can access any uiview object in the application. The uiapplication instance is assigned a proxy object, uiapplicationdelegate, to process application lifecycle events (such as program startup and shutdown), system events (such as incoming calls, alarm records), and so on.

Each program must have only one uiapplication (or its subclass) instance at runtime. You can call [uiapplication sharedapplication] to obtain the pointer of this Singleton instance.

 

Helloworld Program

Open the project file, find the main. m source file in the Other Sources folder, and open it. You will see the following code:

Int main (INT argc, char * argv []) {

NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

Int retval = uiapplicationmain (argc, argv, nil, nil );

[Pool release];

Return retval;

}

Like all C Programs, this is a main function of the program entry (the NSAID utoreleasepool memory automatically recycles the pool, which is not covered in this article ). Next, let's take a look at the uiapplicationmain function and view the iPhone development documentation:

Int uiapplicationmain (
Int argc,
Char * argv [],
Nsstring * principalclassname,
Nsstring * delegateclassname
);

We will not mention the first two parameters. Anyone who has learned C language knows them.
The third parameter isThe uiapplication class name or its subclass name. If it is nil, it is used by default.The name of the uiapplication class.
The fourth parameter is the name of the instantiated object of the protocol uiapplicationdelegate. If it is nil, the delegate object is loaded from the main NIB file. This is the method that the uiapplication object notifies the system to execute when it monitors system changes.
In our generated helloworld application, the third and fourth parameters are both nil, that isUiapplication and helloworldappdelegate.Helloworldappdelegate implements the uiapplicationdelegate protocol and can override all its methods.
When we open the helloworldappdelegate file, we will see this Code:

-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions

{

// Override point for customization after application launch.


Self. Window. rootviewcontroller = self. viewcontroller;

[Self. Window makekeyandvisible];

Return yes;

}

We know that each application has a uiwindow, which manages and coordinates the screen display of the application. Here, the helloworldviewcontroller instance is assigned to the rootviewcontroller of window. The view of rootviewcontroller is used as the first view of uiwindow.
Okay. Now we can add the required controls to the loadview of helloworldviewcontroller:

// Implement viewdidload to do additional setup after loading the view, typically from a nib.

-(Void) loadview

{

// Add Control

}

So far, the startup process and introduction of the helloword program have been completed. Do you have any new knowledge about the startup process?

The process of program startup is summarized as follows:

1. The main function of the program entry creates the uiapplication instance and the uiapplication proxy instance.

2. Rewrite the startup method in the uiapplication proxy instance and set the first viewcontroller.

3. Add controls to the first viewcontroller to implement the application interface.

 

By sschu

Related Article

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.