Re: sheets
Re: sheets
- Subject: Re: sheets
- From: Jeff Harrell <email@hidden>
- Date: Mon, 2 Jun 2003 21:40:36 -0500
For the record, doing this prevents basic stuff about your app from
working, like the application menu. When you're in the middle of
runModalForWindow, you can't hide or quit your application. Which can
be surprising to the user, to say the least.
I find that it's easier--not at first, but once I wrapped my head
around the idea--to encapsulate the display and dismissal of my sheet
in a couple of methods... err, messages... in my controller object. For
example, I might have a method that looks like this:
- (void)displaySheet
{
[NSApp beginSheet:mySheet
modalForWindow:mainWindow
modalDelegate:nil
didEndSelector:nil
contextInfo:nil];
}
And one that looks like this:
- (void)dismissSheet:(id)sender
{
[NSApp endSheet:mySheet];
[mySheet orderOut:self];
}
Invoking the dismissSheet: message from the notification handler is
optional; if you're doing a custom alert sheet, you can make the
methods IBActions and wire the buttons on the sheet directly up to the
methods in the target object.
If you're using the sheet convenience function, though, that's a
different kettle of fish.
If you want to display and handle a sheet in the middle of a
function/method/thing, then this approach requires you to rethink your
code a bit. But in general I don't find that that's necessarily a bad
thing.
On Monday, June 2, 2003, at 08:40 PM, Emma Whan wrote:
>
Although you are probably not meant to do this, if you want to prevent
>
your app from executing while the sheet is on screen and continue
>
execution exactly where you left off when the user dismisses the sheet,
>
you can do so.
>
>
You simply call [NSApp runModalForWindow:yourSheet] right after you
>
display the sheet. In your sheetDidEnd method you then call [NSApp
>
stopModal]. You should not use this approach with a document-based app
>
though as it would prevent the user from doing anything in other
>
document windows. Another downside to doing this is that your main
>
window's close and miniaturize buttons will be disabled after the sheet
>
is dismissed (a Cocoa bug?), so you have to manually re-enable them.
>
>
So when you want to display the sheet, you can use code like this:
>
>
[NSApp beginSheet:mySheet modalForWindow:myWindow modalDelegate:self
>
didEndSelector:@selector(mySheetDidEnd:returnCode:contextInfo:)
>
contextInfo:nil];
>
[NSApp runModalForWindow:mySheet];
>
>
Your sheetDidEnd method could look like this:
>
>
- (void)mySheetDidEnd: (NSWindow *)sheet returnCode: (int)returnCode
>
contextInfo: (void *)contextInfo
>
{
>
[NSApp stopModal];
>
>
//re-enable close and miniaturize buttons as these are disabled
>
after the sheet disappears
>
NSButton *closeButton = [myWindow
>
standardWindowButton:NSWindowCloseButton];
>
NSButton *miniaturizeButton = [myWindow
>
standardWindowButton:NSWindowMiniaturizeButton];
>
[closeButton setEnabled:YES];
>
[miniaturizeButton setEnabled:YES];
>
}
>
You will still, of course, have to order out the sheet when the user
>
clicks the OK button or whatever in your sheet
>
>
[mySheet orderOut: sender];
>
[NSApp endSheet:mySheet];
>
>
On Tuesday, June 3, 2003, at 06:32 AM,
>
email@hidden wrote:
>
>
> On Monday, Jun 2, 2003, at 13:37 US/Eastern, Brent Gulanowski wrote:
>
>
>
>> Not a specific question, more one of strategies. I have been playing
>
>> a
>
>> bit with sheets, and have become confused by how to cope with their
>
>> asynchronous nature. I feel that I am losing the thread of my
>
>> workflow
>
>> when I post a sheet and the method returns immediately. I know that
>
>> it
>
>> calls another method when it ends, but I am confused in that it seems
>
>> like I suddenly have two threads of execution when I only want one.
>
>> The method that called the sheet cannot do anything else, at least
>
>> not
>
>> related to the sheet's purpose, so when it returns, where does
>
>> execution continue from? When the onEnd method returns, where does it
>
>> go?
>
>
>
> There are no threads involved, when the method that begins the sheet
>
> returns, you simply return to the normal event processing (this is
>
> what
>
> allows you to put a sheet on one window, but still accept events in
>
> other windows.
>
>
>
>
>
>> Or should I just continue going about my business from there? But it
>
>> seems that the sheet does not completely get dismissed until the
>
>> onEnd
>
>> method returns, which means the sheet hangs there, waiting. That
>
>> looks
>
>> stupid and annoys the user, so I want to return from the onEnd method
>
>> ASAP.
>
>
>
> Call orderOut: on the sheet in your method to remove it from the
>
> screen
>
> before you return.
>
>
>
>> But how do I get back to doing whatever process I needed to do?
>
>> Where
>
>> does execution go? I guess normally it goes back to the event loop,
>
>> and waits for a user event. The only solution I have come up with is
>
>> that I can post a notification in the onEnd method and pick it up
>
>> somewhere else in my document object. Seems awkward, seeing as how I
>
>> already had the execution thread before I called the sheet.
>
>
>
> --
>
> http://homepage.mac.com/clarkcox3/
>
> email@hidden
>
> Clark S. Cox, III
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >Re: sheets (From: Emma Whan <email@hidden>) |