IOS learning-how to use notifications in iOS, ios notifications

Source: Internet
Author: User
Tags notification center

IOS learning-Notification usage in iOS, ios notification

Notifications and pushes are often used when mobile terminals start to play. For example, notifications are required for text messages, notifications are required for mobile phone horizontal screens, and notifications are required for plug-ins, we can take corresponding actions based on these notifications. The iOS system itself defines the right notifications, but we can also customize some notifications during the development process, and then useNsicationicationcenter sends notifications as needed, and takes corresponding actions when receiving notifications.

The learning of notifications is actually very simple. First, we need to register the notifications so that we can receive the corresponding types of notifications and define the actions taken when the notifications are received; second, we need to learn when to send notifications. Of course, notifications defined by the iOS system are sent spontaneously and do not need to be sent, of course, we can also use this method to simulate sending system-defined notifications. Finally, we need to know how to remove notifications.

Before getting started, let's first take a look at the notifications defined by the system. The official website is defined as follows:NSNotificationName. You can check this information if necessary. Because there are too many notifications, we recommend that you directly ask du Niang if there is a corresponding notification class name when you need to use the notification class, if yes, go to the official website to find the corresponding explanation and description. If not, customize the description.

The following content mainly comes from the article:IOS creates, adds, sends, and removes notifications.

1. Create a notification

It is best to create a notification in the viewDidLoad method.

-(Void) viewDidLoad {[super viewDidLoad]; // create a notification [[nsicationicationcenter defacenter center] addObserver: self selector: @ selector (tongzhi :) name: @ "tongzhi" object: nil];} // Method for receiving notifications-(void) tongzhi :( NSNotification *) notification {NSDictionary * dic = notification. object; // NSLog (@ "notified-dic =%@", notification. object );}
2 send notifications
// Configure the parameter NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "324234", @ "bankId", @ "XX Bank", @ "bankName ", nil]; // send a notification through the notification center [[nsnotifcenter center defacenter center] postNotificationName: @ "tongzhi" object: dic];
3. Removal notification

Based on the principle of who creates and who is responsible, the Controller that creates the notification is generally used to remove the notification,It is best to remove it in the dealloc method.,If the notification cannot be removed in a timely manner, the nsicationicationcenter will be created again the next time you enter the controller, and a notification will be sent to the last created notification in the corresponding method, however, the Controller of the previous notification has been killed, so an error will be reported at this time..

-(Void) dealloc {// method 1. all notifications under this controller can be removed here. // all notifications are removed. NSLog (@ "all notifications removed"); [[nsicationicationcenter defaultCenter] removeObserver: self]; // method 2. here, we can remove the notification NSLog named tongzhi under the Controller // remove the notification named tongzhi (@ "removed the notification named tongzhi "); [[nsicationcenter center defacenter center] removeObserver: self name: @ "tongzhi" object: nil];}

Note:If the dealloc method is not called, the current variable is not released.If the problem cannot be found, you can also rewrite the Controller's return button backBarButtonItem event to remove the notification when returning. In addition, if dealloc is not called, memory leakage exists, which must be solved. During iOS development, the management of Memory leakage is very strict, we must do a good job in this area. For more information about the precautions and methods for Memory leakage problems, see my previous article:IOS learning-Memory leakage check and Cause Analysis

// Return the previous interface event-(void) backpreviusviewcontrolleraction {// method 1. all notifications under this controller can be removed here. // all notifications are removed. NSLog (@ "all notifications removed"); [[nsicationicationcenter defaultCenter] removeObserver: self]; // method 2. here, we can remove the notification NSLog named tongzhi under the Controller // remove the notification named tongzhi (@ "removed the notification named tongzhi "); [[nsicationcenter center defacenter center] removeObserver: self name: @ "tongzhi" object: nil]; // return to the interface [self. navigationController popViewControllerAnimated: YES];}

 

 

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.