Question

I want to seek confirmation with the user that he wants to close the only window and with that the whole app

So far I have this:

- (BOOL)windowWillClose:(id)sender{
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"Yes"];
    [alert addButtonWithTitle:@"No"];
    [alert setMessageText:@"Are you sure you want to quit?"];
    [alert setInformativeText:@"Quiting will stop the machine, please make sure it is back to its origin."];
    [alert setAlertStyle:NSWarningAlertStyle];
    [alert setShowsSuppressionButton:YES];
    NSInteger result = [alert runModal];

    if ( result == NSAlertFirstButtonReturn ) {
        return YES;
    } else {
        return NO;
    }
}

- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {

     return YES;
}

The window and application close before there is a allert, if I remove applicationShouldTerminateAfterLastWindowClosed my window closes, nothing happens. but when I switched it up and put the allert in applicationShouldTerminateAfterLastWindowClosed the allert worked but by than my window is already closed.

I also tried it with windowShouldClose but that didn't work either.

Any ideas on what I am doing wrong here?

Was it helpful?

Solution

Add a delegate that implements the

- (BOOL)windowShouldClose:(id)sender;

delegate method, see here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSWindowDelegate_Protocol/Reference/Reference.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top