Demonstration of Data Transmission Mechanism for iPhone development 01. nsnotification

Source: Internet
Author: User
I. demonstration using nsnotification and uialertveiw: communication between a notification (usually the one that sends the message) and an observer (the one that receives the message. Notifications and observers are two mutually independent classes. Program effect: (1) first create a class inherited from uiviewcontroller: myobserver. h. (As the observer) 1) myobserver. h [Plain]View plaincopy

  1. # Import <uikit/uikit. h>
  2. # Import "myclass. H"
  3. @ Interface myobserver: uiviewcontroller
  4. @ Property (retain) myclass * myclass;
  5. -(Void) updata :( nsnotification *) notifi;
  6. @ End
1) myobserver. m [Plain]View plaincopy

  1. # Import "myobserver. H"
  2. @ Implementation myobserver
  3. @ Synthesize myclass;
  4. -(Void) viewdidload
  5. {
  6. // Note that the myclass must be a global variable and cannot be written like myclass * myclass = [[myclass alloc] init.
  7. Self. myclass = [[myclass alloc] init];
  8. // Define the pop-up dialog box. Note that the delegate: myclass must be myclass so that myclass can perform the operation after clicking the button.
  9. Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "hello" message: @ "dongstone" delegate: myclass cancelbuttontitle: @ "cancle" success: @ "back", nil];
  10. // Add a class to the notification to make it an observer to receive messages. Self indicates the current class. selector: The method to be executed after the notification is received. Name: indicates the pass that is matched when a message is sent (Object: notification within the range. Here object: Nil is a notification that can be sent of any type. Both the name and object conditions must be met.
  11. [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (updata :) name: @ "pass" Object: Nil];
  12. // Display the dialog box
  13. [Alert show];
  14. [Super viewdidload];
  15. // Do any additional setup after loading the view from Its nib.
  16. }
  17. // This method is customized for receiving data
  18. -(Void) updata :( nsnotification *) notifi {
  19. Nsstring * STR = [[notifi userinfo] objectforkey: @ "key"];
  20. Uialertview * Al = [[uialertview alloc] initwithtitle: @ "The message has been passed in." message: Str delegate: Self cancelbuttontitle: Nil otherbuttontitles: Nil];
  21. [Al show];
  22. }
(2) create a common class (myclass. h) (inheriting everything, because this program does not need to have too many functions ). (As notification)

2) myclass. h

 

[Plain]View plaincopy

  1. # Import <Foundation/Foundation. h>
  2. @ Interface myclass: nsobject <uialertviewdelegate>
  3. @ End

2) myclass. m

 

 

 

 

 

[Plain]View plaincopy

  1. # Import "myclass. H"
  2. @ Implementation myclass
  3. -(ID) Init
  4. {
  5. Self = [Super init];
  6. If (Self ){
  7. }
  8. Return self;
  9. }
  10. // Implementation Method
  11. -(Void) alertview :( uialertview *) alertview clickedbuttonatindex :( nsinteger) buttonindex {
  12. // The type that the method userinfo can receive is nsdictionary.
  13. Nsstring * STR = [[nsstring alloc] initwithformat: @ "The subscript of the clicked button is: % d", buttonindex];
  14. Nsdictionary * DIC = [[nsdictionary alloc] initwithobjectsandkeys: Str, @ "key", nil];
  15. // Send the message. @ "pass" matches the notification name, object: Nil notification class range
  16. [[Nsicationcenter center defacenter center] postnotificationname: @ "pass" Object: Nil userinfo: DIC];
  17. }
  18. @ End
Illustration:

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.