2017-05-29 28 views
1

我有以下代碼,它預計顯示在AppDelegate.m工作表警報。可可NSAlert不會顯示警告作爲工作表

- (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; 
    } 
} 

但不幸的是,結果警報並未顯示爲工作表。像截圖一樣。

doesNotShowAlertAsSheet

我不明白爲什麼。並想知道如何將警報顯示爲工作表。請幫幫我!!

+0

你如何檢查它不是工作表? – Sulthan

+1

主窗口是否已經在屏幕上?使用這個主窗口而不是一個不可見的新窗口。 'instantiateControllerWithIdentifier':「這個方法每次調用它時都會創建一個指定控制器的新實例。」 – Willeke

+1

@Willeke感謝您的評論。我使用'[[NSApplication sharedApplication] mainWindow]'而不是'instantiateControllerWithIdentifier'。然後問題解決了!非常感謝!! – xanadu6291

回答

0

它爲我工作。我不太瞭解。

可能是因爲部分beginSheetModalForWindow:

在你的問題中,似乎beginSheetModalForWindow:mainWindowController.window

我改變了它從mainWindowController.windowself.view.window和它的工作,因爲它是如下:

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

可能會幫助我想。