user1411443
user1411443

Reputation:

Proper way of showing a NSWindow as sheet

Function NSBeginAlertSheet(...) has all the events I need to have especially the didDismiss: callback, but I really need to be able to do the same sheet action with any window I want, so I discovered this notification:

NSWindowDidOrderOffScreenAndFinishAnimatingNotification

Which is posted whenever a sheet is closed AND done with animations now, my question is can I use that? Or is there a better way?

I use ARC and I load the windows from .xib using NSWindowController.

Overall what I need is to show a window as sheet and catch all events.

Upvotes: 2

Views: 1754

Answers (3)

Mecki
Mecki

Reputation: 132919

Starting with 10.9, the correct way is to call beginSheet:completionHandler: on a NSWindow object.

This method has the advantage, that the completion handler is a block, so it can keep all the objects alive that are required as long as the sheet is still displayed and once the sheet is done and the block has been executed, the block itself is released and thus all objects it was keeping alive are as well.

To make sure a block keeps objects alive, use the objects within that block or if there is no way to use them in a meaningful fashion, put all of them into a NSMutableArray and within the block call removeAllObjects on that array; this requires the block to keep the array alive and the array keeps the rest alive -> memory management made easy.

Upvotes: 0

Keith Smiley
Keith Smiley

Reputation: 63913

What's wrong with

- (void)beginSheet:(NSWindow *)sheet modalForWindow:(NSWindow *)docWindow modalDelegate:(id)modalDelegate didEndSelector:(SEL)didEndSelector contextInfo:(void *)contextInfo

This calls the optional didEndSelector which should look like this:

- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;

This is all in the NSApplication documentation. There are two methods for ending the sheet:

- (void)endSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode
- (void)endSheet:(NSWindow *)sheet

So you could just do whatever you wanted to right before calling endSheet: or you could in the sheetDidEnd: method.

Edit:

Here is an example project showing that after calling [window orderOut:self] then animation is finished and you can do what you'd like.

Upvotes: 2

user1411443
user1411443

Reputation:

NSWindowDidEndSheetNotification It is posted whenever a sheet is finished animating out.

Upvotes: 0

Related Questions