The method of delegating protocol in Appdelegate and its triggering method using observer pattern

Source: Internet
Author: User



//when an application is about to go into an inactive state, during which time the application does not accept messages or events, such as incoming calls- (void) Applicationwillresignactive: (UIApplication *) Application {NSLog (@"the application is going to be inactive and will be entering the background"); }    //The application is running in the background- (void) Applicationdidenterbackground: (UIApplication *) Application {NSLog (@"if the application supports background running, the application is already running in the background"); If you need to exit the background, add this here: Exit (0);} //The application is about to enter the active state execution- (void) Applicationwillenterforeground: (UIApplication *) Application {NSLog (@"the application will enter the active state and will be running in the foreground"); }    //The application has entered the active state- (void) Applicationdidbecomeactive: (UIApplication *) Application {NSLog (@"the application has entered the foreground and is active"); }    //applications are going to exit, usually used to save the bookshelf to drink some pre-launch cleanup work,- (void) Applicationwillterminate: (UIApplication *) Application {NSLog (@"applications are going to exit, usually used to save the bookshelf to drink some pre-launch cleanup work"); }    //when the device allocates too much memory to the application, the operating system terminates the application and executes the method before terminating .//memory cleanup can usually be done here to prevent the program from being terminated-(void) Applicationdidreceivememorywarning: (UIApplication *) Application {NSLog (@"system memory is low and cleanup is required"); }    //executes when the system time has changed-(void) Applicationsignificanttimechange: (UIApplication *) Application {NSLog (@"executes when the system time has changed"); }    //executes when the program is loaded-(void) Applicationdidfinishlaunching: (UIApplication *) Application {NSLog (@"executes when the program is loaded"); }  

After introducing the method in Appdelegate, go back to the place I want to say

If these methods are triggered in a viewcontroller (such as pressing the home keyboard, the program will enter the background, triggering

Applicationwillresignactive,
Applicationdidenterbackground

method), it is not possible to write logic processing back to appdelegate, so we can use the observer pattern to deal with this problem:

-(void) viewwillappear: (BOOL) animated {    [Super viewwillappear:animated];        [[Nsnotificationcenter defaultcenter]addobserver:self                                            selector: @selector (AA)                                                name: Uiapplicationdidfinishlaunchingnotification                                              object: nil];}

The name you receive here can be a lot:

Uiapplicationdidfinishlaunchingnotification

Uiapplicationdidenterbackgroundnotification

The corresponding methods in Appdelegate are

The method of delegating protocol in Appdelegate and its triggering method using observer pattern

Related Article

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.