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'.

XCode,Interface Builder - drag Menu onto the Menu Bar, rename its Menu Item

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.

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

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.

XCode,Interface Builder, Connect Menu Item to NSObject and set the action

Connect Menu Item to NSObject and set the action

XCode,Interface Builder, Pop-up appears showing the action to set

Pop-up appears showing the action to set

XCode,Interface Builder, Menu Item connected to NSObject and action 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

XCode,Interface Builder, New nib file, empty template

Interface Builder: create new nib file of type Empty

XCode,Interface Builder, save xib file within the xcodeproj.

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'.

XCode,Interface Builder, set the class of the File's Owner

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.

XCode,Interface Builder, connect File's Owner to the NSPanel

Interface Builder: connect File's Owner to the Panel (Window)

XCode,Interface Builder, File's Owner connection to the NSPanel

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.

XCode,Interface Builder, Panel displayed from menu

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.

Download 012-Show-Panel.zip (2.3 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

  • view of the Donegal Shop from the bottom of Shipquay Street, Derry

animatedPaint