Brief analysis of Program initiation principle

Source: Internet
Author: User

When we started learning iOS, the first thing we learned was to create a new project. Every time I wonder why we choose to create a Single View Application project, no code can run the program on the emulator without knocking. To answer this question you need to understand a little bit about the starting principle of the program, you will understand after reading it.

First, set up a Single View Application project to facilitate our research. It can be clear that, whether it is a C language or a Objiective-c language program, the entry of the program runs is the main function. So let's start with the main function.


Create a new project


Main function

Don't understand the code inside the main function?

int uiapplicationmain (int argc, char *argv[], nsstring *principalclassname, NSString * Delegateclassname);  

Hold the option key and click this method uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class])) , from Apple's official help documentation, let's look at this function. The following two parameters of the
Main parsing function:

parameter description
principalclassname Specifies the application class name (symbol of the app), which must be uiapplication (or subclass). If nil, use the UIApplication class as the default value
Delegateclassname Specifies the application's proxy class, which must comply with the Uiapplicationdelegate protocol

Summing up the function of the functions, according to the process of operation, there are about four points:

    • info.plist file, a description of the main nib file is a keyword, and this keyword corresponds to the available nib file to load the file. (if not, the delegate object's Application:didfinishlaunchingwithoptions: method) is called after the program finishes
    • When the program exits normally, the Uiapplicationmain function returns

What is the UIApplication class? What you need to know is that the UIApplication object is the symbol of the application, so every application has its own UIApplication object, and is a singleton (that is, there is only one UIApplication object in an application), using the UIApplication object, can do some application-level operation
。 Most importantly, based on the study of the main function, it can be concluded that the UIApplication object is the first object created by the program!

Let's focus on this article:

    • if applied info.plist file, a description of the main nib file is a keyword, and this keyword corresponds to the available nib file to load the file. (if not, the delegate object's Application:didfinishlaunchingwithoptions: method) is called after the program finishes

Found Infp.plist file, we found the key in main. Because of its existence, our program will go to load main.storyboard This file, after the program is started, we see its white view on the emulator interface.


Info.plist file

, if we delete this main, it is actually deleted the Info.plist file inside the main keyword. Run the program again, you will find your simulator dark a piece, nothing.


Delete the main in the red box!

So, how does the program run here?
Note the text in the parentheses above. If you understand the proxy mechanism: Become an agent of an object, you must abide by its proxy protocol, according to this agreement, you know some methods need you to implement. This is two appdelegate file Origin-originally they are to implement uiapplicationdelegate proxy method. Look at the figure below,


Auto-generated proxy method in APPDELEGATE.M file

We already know that when the app starts to call the delegate object's Application:didfinishlaunchingwithoptions: method, then we try to write some code in this method:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: ( Nsdictionary *) launchoptions {//Override point for customization after application launch. 1. Create Window Self.window = [[UIWindow alloc] Initwithframe:[uiscreen mainscreen].bounds]; 2. Load Storyboard file, create Controller//Name: Storyboard file name/bundle: Main bundle, pass in nil, indicate main bundle uistoryboard *storyboard = [U Istoryboard storyboardwithname:@ "Main" bundle:nil]; Create a controller by storyboard object//Instantiateinitialviewcontroller: Load the storyboard arrow pointing to the controller Uiviewcontroller *ROOTVC = [Storyboa Rd Instantiateinitialviewcontroller]; 3. Set the window's root controller and display the window Self.window.rootViewController = ROOTVC; 4. display window [Self.window makekeyandvisible]; return YES;} 

Re-run the program and you will find that your emulator interface is turned white again. Yes, what you see is the view of the controller in the main.storyboard file. In other words, if we don't change anything, Xcode will automatically generate the code for us. Don't dwell on the code for the time being, and let you understand that this whole process is the key to this article, and I believe it will help you with your future study.

Hope can help you, there are problems can pay attention to me oh, we coding together

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Brief analysis of Program initiation principle

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.