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.

XCode,Interface Builder - set the classes

Interface Builder: set the Classes

Link them as shown.

XCode,Interface Builder,Set the class of NSObject in MainMenu.nib

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.

XCode,Interface Builder, Run NSNotification

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.

Download 017-Comunicate-Two-Views.zip (2.2 MB)

Useful Links



Please send me your comments

If you include your e-mail I may reply!  

Page last modified: 18:58 Sunday 12th. May 2013

Julius Guzy

Paintings & Drawings

  • painting after Leonardo Da Vinci's Battle of Anghiari

animatedPaint