The startup and execution order of iOS applications

Program startup sequence diagram

Specific execution process

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"--- %s ---",__func__); //__func__ Prints the method namereturn YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
     NSLog(@"--- %s ---",__func__);
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
   NSLog(@"--- %s ---",__func__);
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
   NSLog(@"--- %s ---",__func__);
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
  NSLog(@"--- %s ---",__func__);
}


- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
     NSLog(@"--- %s ---",__func__);
}

- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"--- %s ---",__func__);
}
Copy the code

Start the program

-[AppDelegate application:didFinishLaunchingWithOptions:] 
-[AppDelegate applicationDidBecomeActive:] 
Copy the code

Press Command + H + SHIFT

-[AppDelegate applicationWillResignActive:]
-[AppDelegate applicationDidEnterBackground:] 
Copy the code

Click again to enter the program

-[AppDelegate applicationWillEnterForeground:]
-[AppDelegate applicationDidBecomeActive:]
Copy the code

Memory warning

-[AppDelegate applicationDidReceiveMemoryWarning:]
Copy the code

UIViewControllerLife cycle of

// all non-storyboards (xib or xib) take this method - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle) *)nibBundleOrNil { NSLog(@"%s", __FUNCTION__);
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

    }
    returnself; } // storyBoard takes this method - (instancetype)initWithCoder:(NSCoder *)aDecoder {NSLog(@"%s", __FUNCTION__);
    if (self = [super initWithCoder:aDecoder]) {

    }
    returnself; } - (void)awakeFromNib {[super awakeFromNib]; NSLog(@"%s", __FUNCTION__); } // Load the view (from nib by default) - (void)loadView {NSLog(@"%s", __FUNCTION__); self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.view.backgroundColor = [UIColor redColor]; } - (void)viewDidLoad {NSLog(@)"%s", __FUNCTION__); [super viewDidLoad]; } // viewWillAppear - (void)viewWillAppear:(BOOL)animated {NSLog(@)"%s", __FUNCTION__); [super viewWillAppear:animated]; } // View is about to lay out its Subviews - (void)viewWillLayoutSubviews {NSLog(@)"%s", __FUNCTION__); [super viewWillLayoutSubviews]; } // View has laid out its Subviews - (void)viewDidLayoutSubviews {NSLog(@)"%s", __FUNCTION__); [super viewDidLayoutSubviews]; } // viewDidAppear - (void)viewDidAppear:(BOOL)animated {NSLog(@)"%s", __FUNCTION__); [super viewDidAppear:animated]; } // viewWillDisappear - (void)viewWillDisappear:(BOOL)animated {NSLog(@"%s", __FUNCTION__); [super viewWillDisappear:animated]; } // viewDidDisappear:(void)viewDidDisappear:(BOOL)animated {NSLog(@"%s", __FUNCTION__); [super viewDidDisappear:animated]; } // Memory warning occurs - (void)didReceiveMemoryWarning {NSLog(@)"%s", __FUNCTION__); [super didReceiveMemoryWarning]; } // View destroyed - (void)dealloc {NSLog(@"%s", __FUNCTION__);
}
Copy the code

Analysis of the

  • InitWithNibName :bundle: initializes UIViewController, performs critical data initialization, and calls this method whenever a StoryBoard creates a UIViewController. Note: Do not do views here, views are initialized in the loadView method.

  • initWithCoder: If you’re using StoryBoard for view management, your program doesn’t directly initialize a UIViewController, StoryBoard automatically initializes or automatically initializes when segue gets triggered, so initWithNibName:bundle doesn’t get called, But initWithCoder is called.

  • AwakeFromNib When the awakeFromNib method is called, all view outlets and actions are connected but not yet determined. This method counts as suitable for instantiation of a view controller, because something needs to be set up according to user preferences, It can’t be in a storyBoard or xiB, so it can be loaded in the awakeFromNib method.

  • When the loadView method is executed, if the view controller is created through niB, then the view controller has been unfiled from the NIB file and created, and the next task is to initialize the view. The loadView method is called when the view of the UIViewController object is accessed and empty. That’s one difference from the awakeFromNib method. Suppose we release the view property: self.view = nil when handling memory warnings. Therefore, the loadView method may be called multiple times during the life of the view controller. The loadView method should not be called directly, but instead is called by the system, which loads or creates a view and assigns it to the VIEW property of UIViewController. In the process of creating a view, we will first find the corresponding NIB file according to the nibName and then load it. If the nibName is empty or the corresponding NIB file is not found, an empty view is created (this is usually pure code). Note: when overriding the loadView method, do not call the method of the parent class.

  • ViewDidLoad When loadView loads the view into memory, it calls the viewDidLoad method to set it up further. At this point, the view hierarchy is already in memory, and we can usually load various initialization data, initialize, modify constraints, remove views, and so on in this method.

  • The viewWillAppear system, after loading all the data, will display the view on the screen. This method is called first. This is where we usually make further Settings for the view to be displayed. For example, how to display the device in different directions; Set the direction of the status bar and the display style of the view. On the other hand, when the APP has multiple views, this method will also be called when the upper and lower levels of the view switch. If the data needs to be updated when the view is called, it can only be implemented in this method.

  • The viewWillLayoutSubviews view is about to lay out its Subviews. If the view’s bounds change (e.g., the status bar goes from display to display, the view direction changes) and you adjust the position of the Subviews, you can use this method to do all the work you need to do before adjusting the position

  • The viewDidLayoutSubviews view has laid out its Subviews, where you can put the work that needs to be done after the adjustment is complete.

  • ViewDidAppear This method is called when a view is added to the view hierarchy and when multiple views are switched from one view to another, where you can make further changes to the view being displayed.

  • ViewWillDisappear This method is called when the current view is about to be removed or overwritten, but removeFromSuperview has not been called yet.

  • ViewDidDisappear View has disappeared or been overwritten, and removeFromSuperView has been called;

  • The Dealloc view is destroyed, and this time you need to release the objects you created in init and viewDidLoad.

  • DidReceiveMemoryWarning If there is enough memory, the app’s view will usually stay in memory, but if there is not enough memory, some viewController that is not displaying will get a warning that it is out of memory and will release its own view. To achieve the purpose of freeing memory. But the system only frees memory, it doesn’t release ownership of the object, so usually what we need to do here is to release ownership of objects that don’t need to be displayed in memory, and set their pointer to nil.