Example code programming Objective-C with Cocoa in Xcode and Interface Builder (Leopard)
17: Communicate using NSNotification
Communicate between two objects from different untrelated classes and which do not know each other's id.
Create a new project in XCode: File->New Project->Cocoa Application
Call it: 017-Comunicate-Two-Views
Now create the classes: MyMenuControllerOne of type NSMenu and MyMenuControllerTwo of type NSMenu.
Code these as shown below and save.
Note that the classes are quite independent and only linked via NSApplication.
// MyMenuControllerOne.h // 017-Comunicate-Two-Views // #import <Cocoa/Cocoa.h> @interface MyMenuControllerOne : NSMenu { NSNotificationCenter * myNotficationCenter; } - (IBAction)createNotificationOne:(id)pId; - (IBAction)talkToMenuTwo:(id)pId; @end // MyMenuControllerOne.m // 017-Comunicate-Two-Views // #import "MyMenuControllerOne.h" @implementation MyMenuControllerOne - (IBAction)createNotificationOne:(id)pId; { NSLog(@"createNotificationOne"); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"NoteFromTwo" object:nil]; } // end createNotificationOne - (IBAction)talkToMenuTwo:(id)pId; { [[NSNotificationCenter defaultCenter] postNotificationName:@"NoteFromOne" object:@"Hi from MenCont One"]; } // end talkToMenuTwo -(void)handleNotification:(NSNotification *)pNotification { NSLog(@"MenContOne received message = %@",(NSString*)[pNotification object]); } // end handleNotification @end // MyMenuControllerTwo.h // 017-Comunicate-Two-Views // #import <Cocoa/Cocoa.h> @interface MyMenuControllerTwo : NSMenu { NSNotificationCenter * myNotficationCenter; } - (IBAction)createNotificationTwo:(id)pId; - (IBAction)talkToMenuOne:(id)pId; @end // MyMenuControllerTwo.m // 017-Comunicate-Two-Views // #import "MyMenuControllerTwo.h" @implementation MyMenuControllerTwo - (IBAction)createNotificationTwo:(id)pId; { NSLog(@"createNotificationTwo"); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"NoteFromOne" object:nil]; } // end createNotificationTwo - (IBAction)talkToMenuOne:(id)pId; { [[NSNotificationCenter defaultCenter] postNotificationName:@"NoteFromTwo" object:@"Hi from MenCont Two"]; } // end talkToMenuOne -(void)handleNotification:(NSNotification *)pNotification { NSLog(@"MenContTwo received message = %@",(NSString*)[pNotification object]); } // end handleNotification @end
Bring up Interface Builder by double clicking on MainMenu.nib.
Create two Menus with items as shown and set their Classes to MyMenuControllerTwo and MyMenuControllerTwo.
Interface Builder: set the Classes
Link them as shown.
Link the IBActions to the menu items
Save.
Go into XCode and run
Start by clicking on the 'Set-Up Notification' item of each menu.
Then click on the 'Send from Controller' menu items.
Results shown below.
Run
If you want to download the code
Click the Download Link to obtain 017-Comunicate-Two-Views.zip file of this whole OS X 10.5 Leopard program.