How to create custom NSAlert Sheet Method with the Completion Handler paradigm

nsalert, objective-c, objective-c-blocks

Solution

Assuming the code that calls the `confirm:withMoreInfo:andTheActionButtonTitle:` is called from `validate`.

-(void)validate
{
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:questionTitle];
// fill out NSAlert

[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse returnCode) {
    if(returnCode == NSModalResponseStop)
    {
        confirmFlag = YES;
    }
    else
    {
        confirmFlag = NO;
    }
//Rest of your code goes in here.
}];

}

The rest of your code needs to be INSIDE the completion block.

Problem

I have used this simple general method for a While and it works fine for App Based dialogs, however I would like the same functionality in a sheet style dialog and I am having a hard time getting one together. According to the docs as I understand them, the only non deprecated approach from OS10.9 and beyond is to use the NSAlert class with the completion handler process. It seems to make it almost impossible to return a Bool from the general purpose method. My Code: ``` -(BOOL)confirm :(NSString*)questionTitle withMoreInfo:(NSString*)addInfo andTheActionButtonTitle:(NSString*)actionType{ BOOL confirmFlag = NO; NSAlert *alert = [NSAlert alertWithMessageText: questionTitle defaultButton:actionType alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@"%@",addInfo]; [alert setAlertStyle:1]; NSInteger button = [alert runModal]; if(button == NSAlertDefaultReturn){ confirmFlag = YES; }else{ confirmFlag = NO; } return confirmFlag; } The [alert runModal] returns the value I can return. ``` Using the newer paradigm, [alert beginSheetModalForWindow:[self window]sheetWindow completionHandler: some_handler] does not allow me to update or return the value at the end of the method. I know why, but is there a way I'm not thinking of to accomplish this. Please show me how to create a similar method to the one I've ben using for sheets. Thanks Mie

Original source