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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)