IOS uses the notification (NSNotification) for value transfer
A notification is a commonly used value-passing proxy method between jump controllers. In addition to the proxy mode, notifications are more convenient and convenient. A simple Demo is used to implement redirect value transfer for notifications.
Use of iOS notification transfer value
Enter the information to be sent, and pass the label value through the button method,
-(IBAction) buttonClick :( id) sender {
// Add a dictionary and pass the label value through key value settings
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: self. textFieldOne. text, @ textOne, self. textFieldTwo. text, @ textTwo, nil];
// Create a notification
NSNotification * notification = [NSNotification notificationWithName: @ tongzhi object: nil userInfo: dict];
// Send a notification through the notification center
[[Nsicationcenter center defacenter center] postNotification: notification];
[Self. navigationController popViewControllerAnimated: YES];
}
After a notification is sent, register the notification listener in the Controller to receive the notification, and receive the information sent by the notification.
-(Void) viewDidLoad {
[Super viewDidLoad];
// Registration notification
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (tongzhi :) name: @ tongzhi object: nil];
}
-(Void) tongzhi :( NSNotification *) text {
NSLog (@ % @, text. userInfo [@ textOne]);
NSLog (@ ----- receive notification ------);
}
Removal notification: removeObserver: And removeObserver: name: object:
Among them, removeObserver: is to delete all the entries of the observer in the scheduling table saved by the notification center, while removeObserver: name: object: is to delete an entry of the observer in the scheduling table saved by the notification center.
This is relatively simple, just call this method. For example:
[[Nsicationcenter center defacenter center] removeObserver: observer name: nil object: self];
Note that icationicationobserver is the observer to be deleted and cannot be set to nil.