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;
}
}
但不幸的是,結果警報並未顯示爲工作表。像截圖一樣。
我不明白爲什麼。並想知道如何將警報顯示爲工作表。請幫幫我!!
你如何檢查它不是工作表? – Sulthan
主窗口是否已經在屏幕上?使用這個主窗口而不是一個不可見的新窗口。 'instantiateControllerWithIdentifier':「這個方法每次調用它時都會創建一個指定控制器的新實例。」 – Willeke
@Willeke感謝您的評論。我使用'[[NSApplication sharedApplication] mainWindow]'而不是'instantiateControllerWithIdentifier'。然後問題解決了!非常感謝!! – xanadu6291