The complete process of program initiation

Source: Internet
Author: User

The complete process of program initiation
int main(int argc, char * argv[]){    @autoreleasepool {        return UIApplicationMain(argc, argv, nil, NSStringFromClass([MJAppDelegate class])); }}
Execution order
    • 1.main function

    • 2.UIApplicationMain

      • Create a UIApplication object
      • Create a UIApplication delegate object
    • 3. Turn on the main run cycle
    • 3 (1). Delegate object start processing (listen) system events (no storyboard)

      • When the program is started, the agent's application:didfinishlaunchingwithoptions is called: method
      • Create UIWindow in Application:didfinishlaunchingwithoptions:
      • Create and set Rootviewcontroller for UIWindow
      • Display window
    • 3<1>. Info.plist to obtain the most important storyboard file name, load the main storyboard (with storyboard)

    • Create UIWindow
    • Create and set Rootviewcontroller for UIWindow
    • Display window
Window (UIWindow)
    • A special uiview that has at least one UIWindow in an app
    • The first view created after iOS was started is UIWindow
    • Then create the controller, and then add the view of the controller to the UIWindow display.
In - didFinishLaunchingWithOptions:Method in the code creation controller
Create windowSelf.window = [[uiwindow Alloc] Initwithframe:[uiscreen Mainscreen].bounds]; //created with controller uiviewcontroller *VC = [[ uiviewcontroller alloc] init]; //add to the window's heel controller self.window.rootviewcontroller = VC; //the controller's view is added to the window, and not added to the controller can also display //[self.window addsubview: Rootvc.view]; //display window //[self.window makekeywindow];//Set as main window [self.window makekeyandvisible]; //is set to the main window and displays //self.window.hidden = no;//only displays    
Why the window cannot be displayed
    • No strong reference window
    • The size of the window is not set
    • The controller is not added to the window or set to the controller
In - didFinishLaunchingWithOptions:method in which the code loads the storyboard controller
    1. Create a windowSelf. window = [[UIWindow alloc] Initwithframe:[uiscreen Mainscreen]. bounds];2. Load the storyboard file and create the controller//Name: Storyboard file name //Bundle: Main bundle, passing in nil, representing the main bundle Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Main" Bundle:nil"; //Create controller by identity //uiviewcontroller *ROOTVC = [Storyboard instantiateviewcontrollerwithidentifier:@ "VC"]; //instantiateinitialviewcontroller: Load the storyboard arrow pointing to the controller  Uiviewcontroller *ROOTVC = [Storyboard Instantiateinitialviewcontroller]; //3. Sets the root controller of the window and displays the window self.rootviewcontroller = ROOTVC; //4. Display window [self.window Makekeyandvisible];               
In - didFinishLaunchingWithOptions:method in which the code loads the Xib controller
    1. Create a windowSelf. window = [[UIWindow alloc] Initwithframe:[uiscreen mainscreen]. Bounds]; //2. Set the root controller of the window// xib to load the controller //<1> First create a xib file //<2>. Xib file needs to drag a view description controller's view //<3> need to connect the view on the Xib with the controller, set the Xib file ' owner for the controller uiviewcontroller *ROOTVC = [[ Uiviewcontroller alloc] Initwithnibname:@ "VC" bundle:Nil]; self. Window. Rootviewcontroller = ROOTVC; //3. display window [Self. window makekeyandvisible];           
Order of loading Xib files
    • If the load Xib file is passed into the name parameter, the load that is described by the parameter
    • If no parameters are passed in
      • First load the view.xib with the controller name
        • If the controller is named Ylviewcontroller, the Ylview.xib is loaded first
      • And then load the same xib as the control name.
        • If the controller is named Ylviewcontroller, the Ylviewcontrller.xib is loaded first
      • If none are found, the load is empty.
Order of creation of Controller view
    • Priority Layoutview Code creation
    • Storyboard Create
    • Xib Create

The life cycle of the Controller view
    • Loadview
    • Viewdidload (view load complete)
    • Viewwillappear (view is about to show to window)
    • Viewdidappear (view display finished, already displayed to the window)
    • Viewwilldisappear (view is about to be removed from the window and will not be visible)
    • Viewdiddisappear (view is completely removed from window) when there is a memory warning
    • Didreceivememorywarning (When a memory warning is received)
    • Viewwillunload (when view is about to be destroyed) destroys view, first without view
    • Viewdidunload (view destroy complete)
Some life cycle methods for processing applications
Called when the program starts to complete__FUNC__: Indicates in which class the current method is calledCalled when the program has finished loading-(BOOL) Application: (UIApplication *) Application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions {Override point for customization after application launch.ReturnYES;}Called when the application loses focus-(void) Applicationwillresignactive: (UIApplication *) Application {Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state.Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should with this method to pause the game.}Called when the application enters the backgroundSave some data-(void) Applicationdidenterbackground: (UIApplication *) Application {Use the This method to release the shared resources, save user data, invalidate timers, and store enough application state info Rmation to the restore your application to the it is terminated later.If your application supports background execution, this method is called instead of Applicationwillterminate:when the User quits.}Called when the application enters the station-(void) Applicationwillenterforeground: (UIApplication *) Application {Called as part of the transition from the background to the inactive state; Here's can undo many of the changes made on entering the background.}Called when the application gets the focusWhen the user takes full focus, they can interact with the interface-(void) Applicationdidbecomeactive: (uiapplication *) application { //Restart Any tasks this were paused (or not yet started) while the application was Inactive. If the application is previously in the background, optionally refresh the user interface.} call-(void) Applicationwillterminate when the application is closed: (uiapplication *) application { //called when the Application is on to terminate. Save data if appropriate. See also Applicationdidenterbackground:.} //When the program receives a memory warning call-(void) applicationdidreceivememorywarning: (uiapplication *) application{ // Here you can empty the picture cache NSLog (@ "%s", __func__);}           

The complete process of program initiation

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.