【发布时间】:2017-10-29 10:47:31
【问题描述】:
我有以下代码希望在 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;
}
}
但不幸的是,结果警报未显示为工作表。就像截图一样。
我不明白为什么。并且想知道如何将警报显示为工作表。请帮帮我!!
【问题讨论】:
-
如何检查它不是一张纸?
-
主窗口是否已经在屏幕上?使用这个主窗口而不是一个尚不可见的新窗口。
instantiateControllerWithIdentifier: "这个方法每次调用时都会创建一个指定控制器的新实例。" -
@Willeke 感谢您的评论。我使用
[[NSApplication sharedApplication] mainWindow]而不是instantiateControllerWithIdentifier。然后问题就解决了!!非常感谢!!
标签: objective-c cocoa storyboard nsalert