What is the correct way to capture the return code for a warning sheet for OS X 10.9+?

According to Apple's documentation , it seems that the only not obsolete method for displaying a warning sheet is beginSheetModalForWindow:completionHandler: where completionHandler block takes an argument of type NSModalResponse . NSModalResponse is an enumeration with 3 possible values : NSModalResponseStop , NSModalResponseAbort and NSModalResponseContinue . So, how do I know which button on the alert sheet was clicked by the user?

+5
source share
1 answer

The result code is not really an enumeration in the sense that it is not limited to values ​​of type NSModalResponse . Enumeration is used only to determine some of the possible values.

See the documentation for the -addButtonWithTitle: method, for example, which explains the response codes generated from the added button: NSAlertFirstButtonReturn for the first, NSAlertSecondButtonReturn for the second, NSAlertThirdButtonReturn for the third, and NSAlertThirdButtonReturn .

See also the documentation for the -runModal method:

If you alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat: alert alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat: following constants are used to identify the button used to reject the alert: NSAlertDefaultReturn , NSAlertAlternateReturn and NSAlertOtherReturn . Otherwise, the constants described in "Return Values ​​of Buttons" are used.

The same rule that applies to the response code passed to the completion handler that you pass to -beginSheetModalForWindow:completionHandler:

+3
source

Source: https://habr.com/ru/post/1203910/


All Articles