1

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!!

xanadu6291
  • 931
  • 10
  • 20
  • How do you check that it's not a sheet? – Sulthan May 29 '17 at 08:01
  • 1
    Is the main window already on the screen? Use this main window instead of a new one which isn't visible yet. `instantiateControllerWithIdentifier`: "This method creates a new instance of the specified controller each time you call it." – Willeke May 29 '17 at 08:55
  • 1
    @Willeke Thanks for your comment. I use `[[NSApplication sharedApplication] mainWindow]` instead of `instantiateControllerWithIdentifier`. Then the issue was solved!! Many Thanks!! – xanadu6291 May 29 '17 at 10:49

1 Answers1

0

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.

Soorej Babu
  • 350
  • 2
  • 19