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
- # Import <uikit/uikit. h>
- # Import "myclass. H"
- @ Interface myobserver: uiviewcontroller
- @ Property (retain) myclass * myclass;
- -(Void) updata :( nsnotification *) notifi;
- @ End
1) myobserver. m
[Plain]View plaincopy
- # Import "myobserver. H"
- @ Implementation myobserver
- @ Synthesize myclass;
- -(Void) viewdidload
- {
- // Note that the myclass must be a global variable and cannot be written like myclass * myclass = [[myclass alloc] init.
- Self. myclass = [[myclass alloc] init];
- // 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.
- Uialertview * Alert = [[uialertview alloc] initwithtitle: @ "hello" message: @ "dongstone" delegate: myclass cancelbuttontitle: @ "cancle" success: @ "back", nil];
- // 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.
- [[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (updata :) name: @ "pass" Object: Nil];
- // Display the dialog box
- [Alert show];
- [Super viewdidload];
- // Do any additional setup after loading the view from Its nib.
- }
- // This method is customized for receiving data
- -(Void) updata :( nsnotification *) notifi {
- Nsstring * STR = [[notifi userinfo] objectforkey: @ "key"];
- Uialertview * Al = [[uialertview alloc] initwithtitle: @ "The message has been passed in." message: Str delegate: Self cancelbuttontitle: Nil otherbuttontitles: Nil];
- [Al show];
- }
(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
- # Import <Foundation/Foundation. h>
- @ Interface myclass: nsobject <uialertviewdelegate>
- @ End
2) myclass. m
[Plain]View plaincopy
- # Import "myclass. H"
- @ Implementation myclass
- -(ID) Init
- {
- Self = [Super init];
- If (Self ){
- }
- Return self;
- }
- // Implementation Method
- -(Void) alertview :( uialertview *) alertview clickedbuttonatindex :( nsinteger) buttonindex {
- // The type that the method userinfo can receive is nsdictionary.
- Nsstring * STR = [[nsstring alloc] initwithformat: @ "The subscript of the clicked button is: % d", buttonindex];
- Nsdictionary * DIC = [[nsdictionary alloc] initwithobjectsandkeys: Str, @ "key", nil];
- // Send the message. @ "pass" matches the notification name, object: Nil notification class range
- [[Nsicationcenter center defacenter center] postnotificationname: @ "pass" Object: Nil userinfo: DIC];
- }
- @ End
Illustration: