Displaying a Cocoa Window as a Sheet in Xcode 4 (OSX 10.7.2) with ARC

James Bellamy picture James Bellamy · Nov 9, 2011 · Viewed 12k times · Source

I'm trying to get a Login Window to display as a sheet from my MainWindow, but whenever I try to implement the AppKit methods an error always pops up for various indistinguishable reasons.

None of the online guides out there are working, when i apply their code / adapted classes to my own project they never work.

Most of the guides are heavily outdated, including the Apple Documentation. And none of them seem to be compatible with Automatic Reference Counting. Or the Xcode 4 interfaces.

Would someone be able to detail for me in full a guide, for the simplest way of displaying a sheet following a button press on the MainWindow.

Feel free to ask for more information if you need it.

Answer

Anne picture Anne · Nov 9, 2011

Tutorial for Xcode 4

Create new project and add the following to AppDelegate.h and AppDelegate.m.

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {

    IBOutlet NSPanel *theSheet;
}

@property (assign) IBOutlet NSWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;

- (IBAction) showTheSheet:(id)sender {

    [NSApp beginSheet:theSheet
       modalForWindow:(NSWindow *)_window
        modalDelegate:self
       didEndSelector:nil
          contextInfo:nil];

}

-(IBAction)endTheSheet:(id)sender {

    [NSApp endSheet:theSheet];
    [theSheet orderOut:sender];

}

@end

Open the MainMenu.xib.
Use the existing NSWindow.
Make it visible using the following button:

Xcode

Create one new NSPanel.
Add the appropriate NSButtons.

Xcode

Connect Close to the App Delegate.

Xcode

And select endTheSheet.

Xcode

Connect Open to the App Delegate.

Xcode

And select showTheSheet.

Xcode

Connect the App Delegate to the new NSPanel.

Xcode

And select theSheet.

Xcode

Select the NSPanel and disable Visible At Launch. (Essential step!)

Xcode

Now hit run and enjoy the result:

Xcode