Re: stop and wait
Re: stop and wait
- Subject: Re: stop and wait
- From: "Sailesh Agrawal" <email@hidden>
- Date: Sun, 05 Oct 2003 11:22:28 -0400
Normally you would create a sheet and the call [sheet runModal]. This
means that the sheet will be the only window the user can click on.
Also, a call to runModal doesn't return until the user click OK or
Cancel.
Now, let me see if I understand the question correctly. When the user
click new in your app you close the current document and create a new one
right ? So what you want to know is, if the current document has unsaved
changes, and the user gets a "do you want to save your changes message?",
how can you tell if they click yes or no ??
Here's how I would do it. First you want to override the default handler
for the "File | New" command. If you're app is not document based then
I'm not sure how to do that. I guess you could create a custom IBAction
and route the message through interface builder. Otherwise you want to
subclass NSDocumentController and implement newDocument:
newDocument:
- (IBAction)newDocument:(id)sender
An action method invoked by the New menu command,
this method creates a new NSDocument object and adds
it to the list of such objects managed by the receiver. It
invokes openUntitledDocumentOfType:display: with the
document type (first argument) being the first one specified
in the CFBundleDocumentTypes property (defined in Info.plist);
the document type determines the NSDocument subclass
used to instantiate the document object.
When you get this call do the following:
-(IBAaction) newDocument:(id)sender {
[[self currentDocument]
canCloseDocumentWithDelegate:self
shouldCloseSelector:@selector(document:shouldClose:contextInfo:)
contextInfo:NULL];
}
- (void)document:(NSDocument *)doc shouldClose:(BOOL)shouldClose
contextInfo:(void *)contextInfo {
if (shouldClose == NO) {
// the user pressed "Cancel" in the "do you want to save changes
dialog"
// or the user pressed "Cancel" in the "Save changes dialog"
// So we should do nothing.
} else {
[self resetVariables];
// do anything else here
}
}
hope this helps,
Sailesh
On Sun, 5 Oct 2003 16:14:56 +0200, "M. Uli Kusterer"
<email@hidden> said:
>
At 9:44 Uhr -0300 05.10.2003, Mark Davis wrote:
>
>When you go to "File: New" in my application it will do this:
>
>
>
>[mainWindow performClose:self];
>
>[self resetVariables];
>
>
Disregarding why you would want to restrict your application to only
>
one window (which IMHO is needlessly restricting your users), you're
>
trying to tackle this from the wrong end: Instead of doing all of
>
this in your "new" method, change your window/document class. The
>
document is sent a windowDidClose message. Or, you could even do this
>
from your window's or document's dealloc method. Those are only sent
>
when the window actually closes. So just have "new" close the current
>
window, and have the window or document take care of cleaning up
>
behind itself.
>
>
Of course, you'd still have to find a way of creating a new window
>
after closing the old one. At worst, you could create your own
>
performClose:andDoAction: method that saves a constant or a SEL to
>
the action to be performed on dealloc in a member variable of the
>
window. Then, on dealloc, it would look at this variable to find out
>
what action to perform, and call your application's
>
resetVariables:andDoAction: method.
>
>
This will get messier and messier though, as you'd have to make sure
>
this member variable is cleared when the user cancels, so a "close"
>
command or click in the window's close box will not suddenly dispatch
>
a cancelled action.
>
>
It may be easier to replace the entire code that asks for "save
>
changes" with your own code that shows a sheet of your own doing, and
>
having the sheet's message be different for creating files than for
>
regular closing. You could even customize the message to say "save
>
the file before opening a new one?" instead of the default text.
>
>
>I want to know how to wait until this performClose method is done so
>
>I can check if the user hit Cancel (in which case they would be
>
>canceling the "newFile" method.
>
>
But why do you think you need to close the previous document before
>
opening the next one anyway? It is very likely that with a little
>
more careful design, you could support multiple open documents at a
>
time, which restricts your users much less, conforms with the
>
behavior of other applications on the computer, and saves you from
>
having to work out elaborate ways of fighting AppKit's default
>
behavior.
>
>
Why do you need to force a single open document? Are you using the
>
document-based style of application? That will easily take care of
>
switching in/out documents, and if you need to keep some "inspector"
>
window or hardware device synced up with the frontmost document, you
>
can easily do so by sending messages to the application (or better, a
>
dedicated delegate for this) when your window comes to the front.
>
>
>I tried doing a while with the returncode of the "Would you like to
>
>save your changes" sheet but while seems to halt everything up so
>
>you can't click anything in the sheet.
>
>
You're not processing any events in your "while" loop, which is what
>
you need to have the user's actions processed by the application. You
>
could run your own run loop, but this can get messy quickly and is
>
much more involved than taking a step back to find a different way of
>
handling the behavior you need.
>
--
>
Cheers,
>
M. Uli Kusterer
>
------------------------------------------------------------
>
"The Witnesses of TeachText are everywhere..."
>
http://www.zathras.de
>
_______________________________________________
>
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.