Re: how does canCloseDocumentWithDelegate work?
Re: how does canCloseDocumentWithDelegate work?
- Subject: Re: how does canCloseDocumentWithDelegate work?
- From: Sherm Pendley <email@hidden>
- Date: Sun, 6 Jul 2003 20:01:56 -0400
On Sunday, July 6, 2003, at 06:47 PM, matt neuburg wrote:
- (void) canCloseDocumentWithDelegate: (id) delegate
shouldCloseSelector:
(SEL) shouldCloseSelector contextInfo: (void *) contextInfo
What I don't understand is how that can prevent the document from
closing,
since a void is not a BOOL. How does this routine communicate back to
the
framework that one should not close?
If you're calling this method from a controller, the BOOL you're looking
for is passed to the callback method you provide, instead of being
returned directly.
// This snippet will presumably be part of a larger action method
[document canCloseDocumentWithDelegate:self
shouldCloseSelector:@selector(document:shouldClose:contextInfo:)
contextInfo:NULL];
// Here is the callback method, which is assumed to be an instance
method belonging
// to the same controller class the above action method is found in.
- (void) document:(NSDocument*)doc shouldClose:(BOOL)shouldClose
contextInfo:(void*)contextInfo {
if (YES == shouldClose) {
[doc close];
}
}
The above is the simplest case; in a more complex scenario, such as a
controller method that checks all open documents before exiting the
application, your callback may want to set a flag in an instance
variable if even one document indicates that it shouldn't be closed. It
could even be an all-or-nothing proposition; the callback could simply
set the failure flag according to the value of shouldClose, and then the
calling code would close all the documents if none of them set the
failure flag.
Conversely, if you're overriding this method in an NSDocument subclass,
you need to pass YES or NO to the callback, rather than returning it
directly to the caller. This is made more complicated by the fact that
the selector you need to call is a variable, and that it takes
non-object parameters. Thus, a simple performSelector:withObject:...
message won't suffice - you need to create an NSInvocation and invoke
it, like this:
- (void)canCloseDocumentWithDelegate: (id)delegate
shouldCloseSelector: (SEL)shouldCloseSelector
contextInfo: (void*)contextInfo {
NSMethodSignature *ms;
NSInvocation *inv;
BOOL shouldClose;
// Do whatever document-specific processing is needed here. If the
document
// is not dirty, shouldClose can be set to YES. If the document is
dirty, you can
// present a sheet or dialog asking if the user wants to save it,
and set shouldClose
// according to the results of that.
// Just to be paranoid, verify that the delegate responds to the
given selector
if ([delegate respondsToSelector:shouldCloseSelector]) {
// Create the invocation object
ms = [delegate methodSignatureForSelector:shouldCloseSelector];
inv = [NSInvocation invocationWithMethodSignature:ms];
// Set the target, selector, and parameters
[inv setTarget:delegate];
[inv setSelector:shouldCloseSelector];
[inv setArgument:&delegate atIndex:2];
[inv setArgument:&shouldClose atIndex:3];
[inv setArgument:&contextInfo atIndex:4];
// Invoke!
[inv invoke];
}
}
Disclaimer: Code typed into email, untested, etc.
sherm--
Though a program be but three lines long, someday it will have to be
maintained.
-- The Tao of Programming
_______________________________________________
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.