Example code programming Objective-C with Cocoa in Xcode and Interface Builder (Leopard)

9: NSSavePanel and NSOpenPanel

Implement a Save File Panel and an Open File Panel using NSSavePanel, NSOpenPanel.

Create a new project in XCode: File->New Project->Cocoa Application
Call it: 009-NSSavePanel

Now create the class: MySaveMenuController of type NSObject.
Code it as shown below and save.

// MySaveMenuController.h // 009-NSSavePanel #import <Cocoa/Cocoa.h> @interface MySaveMenuController : NSMenu { } - (IBAction)doSaveAs:(id)pId; - (IBAction)doOpen:(id)pId; @end // MySaveMenuController.m // 009-NSSavePanel #import "MySaveMenuController.h" @implementation MySaveMenuController - (IBAction)doSaveAs:(id)pId; { NSLog(@"doSaveAs"); NSSavePanel *tvarNSSavePanelObj = [NSSavePanel savePanel]; int tvarInt = [tvarNSSavePanelObj runModal]; if(tvarInt == NSOKButton){ NSLog(@"doSaveAs we have an OK button"); } else if(tvarInt == NSCancelButton) { NSLog(@"doSaveAs we have a Cancel button"); return; } else { NSLog(@"doSaveAs tvarInt not equal 1 or zero = %3d",tvarInt); return; } // end if NSString * tvarDirectory = [tvarNSSavePanelObj directory]; NSLog(@"doSaveAs directory = %@",tvarDirectory); NSString * tvarFilename = [tvarNSSavePanelObj filename]; NSLog(@"doSaveAs filename = %@",tvarFilename); } // end doSaveAs - (IBAction)doOpen:(id)pId; { NSLog(@"doOpen"); NSOpenPanel *tvarNSOpenPanelObj = [NSOpenPanel openPanel]; NSInteger tvarNSInteger = [tvarNSOpenPanelObj runModalForTypes:nil]; if(tvarNSInteger == NSOKButton){ NSLog(@"doOpen we have an OK button"); } else if(tvarNSInteger == NSCancelButton) { NSLog(@"doOpen we have a Cancel button"); return; } else { NSLog(@"doOpen tvarInt not equal 1 or zero = %3d",tvarNSInteger); return; } // end if NSString * tvarDirectory = [tvarNSOpenPanelObj directory]; NSLog(@"doOpen directory = %@",tvarDirectory); NSString * tvarFilename = [tvarNSOpenPanelObj filename]; NSLog(@"doOpen filename = %@",tvarFilename); } // end doOpen @end

Bring up Interface Builder by double clicking on MainMenu.nib.

Select the File menu. Inside the Class menu of the Identity panel of the inspector choose MySaveMenuController. You will see the doSaveAs and doOpen in the actions section.

XCode,Interface Builder - NSMenu with it's subclass

Interface Builder: NSMenu object, its class and actions

In the connections panel of the Inspector drag from the round circles of doSaveAs and doOpen to the SaveAs and Open menu items respectively.

XCode,Interface Builder, link menu items to SaveAs and Open actions

Interface Builder link menu items to SaveAs and Open actions

XCode,Interface Builder, linked menu items to SaveAs and Open actions

Interface Builder linked menu items to SaveAs and Open actions

Save Interface Builder.
Return to XCode, build and run.

Do File->SaveAs and the Save panel will appear.
Choose the directory and file you want and click OK and the path to the file and to the directory name will be shown in the Console.

Do File->Open and the Open panel will appear.
Choose the directory and file you want and click OK and the path to the file and to the directory name will be shown in the Console.

XCode,Interface Builder, run the program with NSSavePanel

Run : NSSavePanel

XCode,Interface Builder, run the program with NSOpenPanel

Run : NSOpenPanel

XCode,Interface Builder, NSSavePanel-NSOpenPanel console log

Console Log showing NSSavePanel and NSOpenPanel output

If you want to download the code

Click the Download Link to obtain 009-NSSavePanel.zip file of this whole OS X 10.5 Leopard program.

Download 009-NSSavePanel.zip (6.6 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