【问题标题】:Cocoa NSAlert does not show alert as sheetCocoa NSAlert 未将警报显示为工作表
【发布时间】: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


【解决方案1】:

它对我有用。我不太了解。

可能是因为beginSheetModalForWindow:这个部分

在你的问题中,beginSheetModalForWindow:mainWindowController.window

我将它从 mainWindowController.window 更改为 self.view.window 并且它起作用了,如下所示:

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

也许它会帮助我思考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-31
    • 2012-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    相关资源
    最近更新 更多