IOS uses the notification (NSNotification) for value transfer

Source: Internet
Author: User
Tags notification center

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.

 

 

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.