Re: NSBeginAlertSheet and (void*)contextInfo
Re: NSBeginAlertSheet and (void*)contextInfo
- Subject: Re: NSBeginAlertSheet and (void*)contextInfo
- From: Jeremy Dronfield <email@hidden>
- Date: Sun, 21 Jul 2002 13:36:17 +0100
On Sunday, July 21, 2002, at 06:20 am, Famille GOUREAU-SUIGNARD wrote:
does anibody know how to handle the (void*)contextInfo parameter of
NSBeginAlertShhet ?
I don't understant how to pass the parameter, and how to retrieve it.
AppKit Functions says "contextInfo [passed from NSBeginAlertSheet] is
the same contextInfo passed into NSBeginAlertSheet". This certainly is
vague and confusing. As I understand it, the purpose of the contextInfo
parameter is to temporarily enable reference to instance variables which
may not belong to the class in which the AlertSheet is created. The
following example (from the Vermont Recipes tutorial) shows how this
works.
- (BOOL)sheetForZeroTextField:(NSControl *)control name:(NSString
*)fieldName {
NSString *alertMessage;
NSString *alertInformation = NSLocalizedString(@"A value of 0.0 is
not valid in this field.", @"Informative text for alert posed by any
text field if 0.0 when attempting to resign first responder status");
NSString *defaultButtonString = NSLocalizedString(@"Edit", @"Name of
Edit button");
NSString *otherButtonString = NSLocalizedString(@"Cancel", @"Name of
Cancel button");
if (fieldName == NULL) {
alertMessage = NSLocalizedString(@"The field value is 0.0.",
@"Message text for alert posed by any unnamed field if 0.0 when
attempting to resign first responder status");
} else {
alertMessage = [NSString
stringWithFormat:NSLocalizedString(@"The %@ field value is 0.0.",
@"Message text for alert posed by any named text field if 0.0 when
attempting to resign first responder status"), fieldName];
}
NSBeginAlertSheet (alertMessage, defaultButtonString, nil,
otherButtonString, [self window], self,
@selector(zeroTextFieldSheetDidEnd:returnCode:contextInfo:), NULL,
control, alertInformation);
return NO;
}
=> When the sheet ends, the following method is called (identified by
@selector in the above NSBeginAlertSheet):
- (void)zeroTextFieldSheetDidEnd:(NSWindow *)sheet
returnCode:(int)returnCode contextInfo:(void *)contextInfo {
if (returnCode == NSAlertOtherReturn) {
NSTextField *field = (NSTextField *)contextInfo; <= CONTEXTINFO
[field abortEditing];
[field selectText:self];
}
}
Here, contextInfo is used quite simply in a convenience constructor to
allow manipulation of the NSTextField whose invalid entry triggered the
AlertSheet. It becomes active when the user clicks the Cancel button in
the sheet, resetting the text in the TextField to what it was before
editing began.
Hope this makes it clearer.
- Jeremy.
_______________________________________________
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.