xanadu6291
xanadu6291

Reputation: 931

Cocoa NSAlert does not show alert as sheet

I have the following code which expects to show alert as sheet in AppDelegate.m.

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {

    if ([self.socket.inputStream streamStatus] == 2) {

        NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
        NSWindowController *mainWindowController = [storyBoard instantiateControllerWithIdentifier:@"MainWindow"];

        NSAlert *alert = [[NSAlert alloc] init];
        [alert addButtonWithTitle:@"OK"];
        [alert setMessageText:NSLocalizedString(@"Warning", @"Warning")];
        [alert setInformativeText:NSLocalizedString(@"Disconnect before quit this app!!", @"Disconnet before quit")];
        [alert beginSheetModalForWindow:mainWindowController.window completionHandler:^(NSModalResponse returnCode) {

        }];

        return NO;

    } else {

        return YES;
    }
}

But unfortunately, the result alert does not shown as sheet. Like the screenshot.

doesNotShowAlertAsSheet

I can't understand why. And would like to know how can I show alert as sheet. Please help me!!

Upvotes: 1

Views: 502

Answers (1)

Soorej Babu
Soorej Babu

Reputation: 368

It worked for me. I don't know much about.

May be it is because of the part beginSheetModalForWindow:

In your question, it seems that beginSheetModalForWindow:mainWindowController.window

I changed it from mainWindowController.window to self.view.window and it worked, as it is given below:

[alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode)
{
    ....
}];

May be it will help i think.

Upvotes: 0

Related Questions