IOS: Creates notifications, sends notifications, and removes notifications. ios removes notifications.

Source: Internet
Author: User
Tags notification center

IOS: Creates notifications, sends notifications, and removes notifications. ios 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

NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "324234", @ "bankId", @ "XX Bank", @ "bankName", nil]; // send a notification through the notification center [[NSNotificationCenter defacenter center] postNotificationName: @ "tongzhi" object: dic];

3. Remove the notification, which is created by the Controller and removed by the Controller. It is best to remove the notification in the dealloc method. If the notification cannot be removed in time, when you enter the Controller next time, the nsicationcenter center will be created again, and a notification will be sent to the last created notification in the corresponding method. However, the controller where the previous notification is located has been killed, so an error will be reported.

-(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 backBarButtonItem event returned by the Controller, remove notification when returning

// 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.