For an iOS app, the life cycle is a very important thing. For an app to control the app's opening, sleep, shutdown and other states, for a page, control page loading, display, disappear; For a view or an ordinary class, control initialization, drawing, and releasing.
First, Appdelegate
When you create an iOS project, in Appdelegate you will see the following six ways to control different app lifecycles, respectively.
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions;
This method is called when the app finishes loading. This method is only executed once when the app is started. The root window of the generic app is created in this method. The app is activated after the call. Launchoptions contains a lot of user information, you can get relevant user information through key such as Uiapplicationlaunchoptionsremotenotificationkey.
Example (non-storyboard/xib mode):
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{ nsdictionary* userInfo = [Launchoptions objectforkey:uiapplicationlaunchoptionsremotenotificationkey ]; = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; = [[Viewcontroller alloc] init]; = Self.viewcontroller; [Self.window makekeyandvisible]; return YES;}
-(void) Applicationwillresignactive: (uiapplication *) application;
This method is called when the app is in use, encounters a call, locks the screen, goes back to the home page, jumps to another app, and the app hangs without running in the background. The app is in a pending state after the call. This method is tuned before Applicationdidenterbackground.
-(void) Applicationdidenterbackground: (uiapplication *) application;
This method is called when the app is in use, encounters a call, locks the screen, goes back to the home page, jumps to another app, and the app needs to run in the background (Apple allows Local,music,voip,notifaction to run in the background). The app is running in the background after the call.
-(void) Applicationwillenterforeground: (uiapplication *) application;
This method is called when the app switches from the background run state to the foreground run. The app is inactive after it is called. This method is tuned before applicationdidbecomeactive.
-(void) Applicationdidbecomeactive: (uiapplication *) application;
This method is called when the app is reactivated. This method is applied before the app has been opened, but not in the active state, where it jumps to the active state. This method will be called every time you jump into the app. The app is activated after the call.
-(void) Applicationwillterminate: (uiapplication *) application;
This method is called when the phone is turned off or when you double-click the Home button to drag the app up so that it exits completely. After this method is executed, the app's process is killed.
Second, Uiviewcontroller
For a page, you have 8 life cycles to control
-(void) awakefromnib;
However, when using storyboard or Xib, this method is called before reading the above layout. This method is earlier than other method calls, and is later than initialized. In a page, this method is only called once.
-(void) viewdidload;
This method is called after reading the layout of storyboard or xib. This method is later than the awakefromnib call, which is earlier than the other method. In a page, this method is only called once. Many are used to initialize page layouts and global layout-related variables.
-(void) Viewwillappear: (BOOL) animated;
This method is called when the page is about to appear. This method is called when the animation starts when the page jumps between pages.
-(void) Viewdidappear: (BOOL) animated;
This method is called when the page is already fully present. This method is called at the end of the animation when jumps between pages are animated.
-(void) Viewwilldisappear: (BOOL) animated;
This method is called when the page is about to disappear. This method is called when the animation starts when the page jumps between pages.
-(void) Viewdiddisappear: (BOOL) animated;
This method is called when the page has completely disappeared. This method is called at the end of the animation when jumps between pages are animated.
-(void) viewwilllayoutsubviews;
This method is called when the page layout is about to change (much for horizontal screen flipping), and this method is called when the rollover animation begins.
-(void) viewdidlayoutsubviews;
This method is called when the page layout changes end (much for the horizontal screen flip), and this method is called when the rollover animation ends.
Third, View & NSObject
-(void) layoutsubviews;
This method is called when the view is called Addsubview, the frame of the view is reset, a uiscrollview is scrolled, and the screen is rotated. Changing the size of a uiview also triggers the Layoutsubviews event on the parent UIView.
-(void) DrawRect: (cgrect) rect;
This method is called when a view is redrawn.
-(void) dealloc;
This method is called when a View or NSObject is released. Variables such as nsnotification that may cause wild pointers and notifications need to be released manually here.
Life cycle in iOS