4

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

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Miek
  • 1,127
  • 4
  • 20
  • 35

1 Answers1

6

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.

lead_the_zeppelin
  • 2,017
  • 13
  • 23
  • 1
    Nice example but it doesn't solve the problem of being able to call a general dialog if the code has to be put in the block. I want to call the same method as a confirmation when a User presses the Save button, or perhaps a delete button. Having to complete the code in a block rather than being able to simply return confirmFlag is my problem. Is there no way to do that? – Miek Apr 23 '14 at 18:05
  • I don't think so. Going from the previous synchronous solution to the current asynchronous NSAlert will need a rethink of your code structure. – lead_the_zeppelin Apr 23 '14 at 18:10
  • I was afraid of that. Okay. Thanks – Miek Apr 23 '14 at 18:12