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];}