Example code programming Objective-C with Cocoa in Xcode and Interface Builder (Leopard)
12: Display a panel by clicking a menu item
Display a panel by clicking a menu item.
The example is presented in two formats: Concise Text which has no illustrations and Illustrated Text.
The reason is that I frequently found illustrations to get in the way of learning.
A 2.3MB .zip file of this OS X 10.5 Leopard program may be downloaded by clicking
here.
All the text and example code is licenced under GPL.
Concise Text (Illustrated text is below)
Create a new project in XCode: File->New Project->Cocoa Application
Call it: 012-Show-Panel
Now create the classes: TheAppController of type NSObject and TheWindowController of type NSWindowController.
Code these as shown below and save.
NOTE: This was implemented in XCode 3.1. I could not make it work in 3.0.
// TheAppController.h // 012-Show-Panel // #import <Cocoa/Cocoa.h> @class TheWindowController; @interface TheAppController : NSObject { TheWindowController *myTheWindowController; } - (IBAction)showTheWindow:(id)pId; @end // TheAppController.m // 012-Show-Panel // #import "TheAppController.h" #import "TheWindowController.h" @implementation TheAppController - (IBAction)showTheWindow:(id)pId; { NSLog(@"Hi from TheAppController showTheWindow"); if (! myTheWindowController ) { myTheWindowController = [[TheWindowController alloc] init]; } // end if [myTheWindowController showWindow:self]; } // end showTheWindow @end // TheWindowController.h // 012-Show-Panel // #import <Cocoa/Cocoa.h> @interface TheWindowController : NSWindowController { } @end // TheWindowController.m // 012-Show-Panel // #import "TheWindowController.h" @implementation TheWindowController - (id) init { if ( ! (self = [super initWithWindowNibName: @"TheNibWindow"]) ) { NSLog(@"init failed in TheWindowController"); return nil; } // end if NSLog(@"init OK in TheWindowController"); return self; } // end init - (void)windowDidLoad { NSLog(@"TheWindowPanel did load"); } // end windowDidLoad @end
Bring up Interface Builder by double clicking on MainMenu.nib.
Drag a Menu from the Library onto the Menu Bar.
Rename its Menu item to 'Open Panel'.
Drag an NSObject from the library onto the MainMenu.nib window.
In the Inspector set the class to 'TheAppController'.
The title 'TheAppController' will now appear under the NSObject icon in the MainMenu.nib window.
Click on the item 'Open Panel' in your app's Menu Bar.
In the connections pane of the inspector, drag from the 'selector' item under the heading 'Sent Actions'
to the 'TheAppController' in the MainMenu.nib window and let go.
A pop-up will appear saying 'showTheWindow'. Click on it and the connection will appear in the Inspector.
Save InterfaceBuilder.
In InterfaceBuilder Menu Bar do File->New.
Choose the Empty template.
Save it as 'TheNibWindow' and also include it within the 012-Show-Panel.xcodeproj
Drag an NSPanel from the library onto the screen.
Click on the File's Owner to select it.
In the Inspector set its class to 'TheWindowController'.
In the Connections panel drag from window to the Panel (Window) icon in the MainMenu.nib window.
This sets the link.
Save the file as an .xib file.
Return to XCode and run.
Click on the menu item and the panel will appear.
Illustrated Text
Create a new project in XCode: File->New Project->Cocoa Application
Call it: 012-Show-Panel
Now create the classes: TheAppController of type NSObject and TheWindowController of type NSWindowController.
Code these as shown above and save.
NOTE: This was implemented in XCode 3.1. I could not make it work in 3.0.
Bring up Interface Builder by double clicking on MainMenu.nib.
Drag a Menu from the Library onto the Menu Bar.
Rename its Menu item to 'Open Panel'.
Interface Builder: drag Menu onto the Menu Bar and rename its Menu Item
Drag an NSObject from the library onto the MainMenu.nib window.
In the Inspector set the class to 'TheAppController'.
The title 'TheAppController' will now appear under the NSObject icon in the MainMenu.nib window.
Set the class of NSObject in MainMenu.nib
Click on the item 'Open Panel' in your app's Menu Bar.
In the connections pane of the inspector, drag from the 'selector' item under the heading 'Sent Actions'
to the 'TheAppController' in the MainMenu.nib window and let go.
A pop-up will appear saying 'showTheWindow'. Click on it and the connection will appear in the Inspector.
Connect Menu Item to NSObject and set the action
Pop-up appears showing the action to set
Menu Item connected to NSObject and the action set
Save InterfaceBuilder.
In InterfaceBuilder Menu Bar do File->New.
Choose the Empty template.
Save it as 'TheNibWindow' and also include it within the 012-Show-Panel.xcodeproj
Interface Builder: create new nib file of type Empty
Interface Builder: save xib file within the xcodeproj
Drag an NSPanel from the library onto the screen.
Click on the File's Owner to select it.
In the Inspector set its class to 'TheWindowController'.
Interface Builder: set the class of the File's Owner
In the Connections panel drag from window to the Panel (Window) icon in the MainMenu.nib window.
This sets the link.
Interface Builder: connect File's Owner to the Panel (Window)
Interface Builder: File's Owner connection to the panel
Save the file as an .xib file.
Return to XCode and run.
Click on the menu item and the panel will appear.
Interface Builder: Panel displayed from menu
If you want to download the code
Click the Download Link to obtain 012-Show-Panel.zip file of this whole OS X 10.5 Leopard program.