Re: Return values of NSAlert
Re: Return values of NSAlert
- Subject: Re: Return values of NSAlert
- From: Ken Thomases <email@hidden>
- Date: Tue, 21 Oct 2014 23:40:09 -0500
On Oct 21, 2014, at 10:59 PM, Gerriet M. Denkmann <email@hidden> wrote:
> NSAlert has: - (void)beginSheetModalForWindow:(NSWindow *)sheetWindow
> completionHandler:(void (^)(NSModalResponse returnCode))handler
>
> NSModalResponse has three values: Stop, Abort, Continue - none of which bear any resemblance with the buttons: Default, Alternate, Other in my NSAlert.
>
> Also the actually returned values are not among those in NSModalResponse.
>
> They look like NSAlertDefaultReturn, NSAlertAlternateReturn instead - but this clearly cannot be the case, as these values are deprecated: "These constants are deprecated in 10.9 and will be formally deprecated in the following release"
>
> The suggested alternative is NSAlertFirstButtonReturn, etc., which have values, which very much unlike the actual returned values.
>
> And the header mentions:
> "Note that these return values (i.e. NSAlertFirstButtonReturn etc. ) do not apply to an NSAlert created via +alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat:, which instead uses the same return values as NSRunAlertPanel. See NSAlertDefaultReturn, etc. in NSPanel.h"
>
> Which brings us back to the already deprecated values.
>
> I am mightily confused.
NSModalResponse is not a proper enum. It is merely an integer type. There's an enum which outlines some possible values, but not the only values.
If you use +alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat:, which you shouldn't, the alert will generate return codes NSAlertDefaultReturn, NSAlertAlternateReturn, or NSAlertOtherReturn. This is documented in the reference for that method.
If you create the alert using +alloc and -init, as you should, then it will generate return codes as documented in the -addButtonWithTitle: method: NSAlertFirstButtonReturn, NSAlertSecondButtonReturn, NSAlertThirdButtonReturn, NSAlertThirdButtonReturn + n, ….
The values NSModalResponseStop, NSModalResponseAbort, and NSModalResponseContinue are for when you run a custom window in modal mode using -[NSApplication runModalForWindow:] or -beginModalSessionForWindow:/-runModalSession:/-endModalSession:. The first two result from -stopModal and -abortModal, respectively. The third is when -runModalSession: returns to your code to allow it to do some work but needs it to continue calling -runModalSession: because the modal session isn't complete yet.
There are also NSModalResponseOK and NSModalResponseCancel, NSFileHandlingPanelOKButton and NSFileHandlingPanelCancelButton.
Finally, you can use custom codes, as documented in -[NSApplication stopModalWithCode:] and -[NSWindow endSheet:returnCode:]. So long as the code which calls these methods and the code which handles the result code agree on the meaning, you're fine.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden