What is the iOS program startup 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, Create 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);

Press and hold the Option key to click this method uiapplicationmain (argc, argv, nil, nsstringfromclass ([appdelegate Class]) , with the official Help documentation for Apple , we'll look at this Function. The following two parameters of the
main parsing function:

parameters

principalclassname

app ), The class must be uiapplication ( or subclass ) . If nil, uses the uiapplication class as the default value

delegateclassname

uiapplicationdelegate protocol

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

An object that instantiates a uiapplication class or its subclasses according to principalclassname, according to the delegateclassname Create a delegate object and assign the delegate object to Uiapplicationd object that Delegate Property.

the application will then be built Main runloop(event loop) to handle the Event.

If there is a keyword in the Application's info.plist file that describes the main nib file, and this keyword corresponds to the available nib file, the file is Loaded. ( if not, the delegate object's application is called after the program finishes : Didfinishlaunchingwithoptions: Method )

when the program exits normally The Uiapplicationmain function does not return

What is UIApplication class ? We don't tangle, what you need to know is that uiapplication objects are symbols of an application, so each application has its own uiapplication object, and is a singleton (that is, there is only one application uiapplication object), using uiapplication object that can perform some application-level operations
. Most importantly, based on the study of the main function, it can be concluded thattheuiapplication object is the first object created by the program!

Let's focus on this article:

If there is a keyword in the Application's info.plist file that describes the main nib file, and this keyword corresponds to the available nib file, the file is Loaded. ( if not, the delegate object's application is called after the program finishes : Didfinishlaunchingwithoptions: Method )

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 actually work here?
Notice 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 the origin of the two appdelegate files-the Original is to implement the Uiapplicationdelegate proxy method. Look at the picture below,

auto-generated Proxy method in APPDELEGATE.M file

we already know that the application:didfinishlaunchingwithoptions: method of The delegate object is called when the app starts to complete, So we're trying 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. Loading Storyboard files, Creating Controllers

Name: is Storyboard file name

Bundle: main bundle, passing in Nil, representing the main bundle

Uistoryboard *storyboard = [uistoryboard storyboardwithname:@ "Main" bundle:nil];

Creating a controller from a storyboard object

Instantiateinitialviewcontroller: Load The controller that the storyboard arrow points to

Uiviewcontroller *ROOTVC = [storyboard instantiateinitialviewcontroller];

3. sets the root controller of the window and displays 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.

Finally: What if you still can't read the code? It doesn't matter, this is the key to our further study, I will continue to write some relevant text to let you know. If you are a great god, thank you for taking the time to get Here. finally, Perhaps I have the understanding of the place or wrong places, you are welcome to criticize Correct.


original from: Jane book /madagascar Ape

What is the iOS program startup 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.