How to create a custom NSAlert Sheet method using the completion handler paradigm

I used this simple general method for While, and it works great for application-based dialogs, however, I would like to have the same functionality in the sheet style dialog style, and it's hard for me to handle it.

According to the docs, as I understand them, the only inexhaustible approach from OS10.9 and higher is to use the NSAlert class with the completion handler process. It seems almost impossible to return Bool from a general-purpose method.

My code is:

-(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.

, [alert beginSheetModalForWindow: [self window] sheetWindow completeHandler: some_handler] . , , , .

, , , .

Mie

+4
1

, , confirm:withMoreInfo:andTheActionButtonTitle:, 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.
}];

}

INSIDE .

+6

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


All Articles