检查NSAlert是否正在显示

3

我在我的应用程序的主屏幕上使用一个NSAlert来显示错误消息。基本上,NSAlert是我的主视图控制器的一个属性。

class ViewController: NSViewController {

    var alert: NSAlert?

    ...

}

当我收到一些通知时,我会展示一些消息。

func operationDidFail(notification: NSNotification)
{
    dispatch_async(dispatch_get_main_queue(), {

        self.alert = NSAlert()
        self.alert.messageText = "Operation failed"
        alert.runModal();
    })
}

现在,如果我收到多个通知,每个通知都会出现警报。我的意思是,它会显示第一条消息,我点击“确定”,它就消失了,然后再次显示第二个消息等等...这是正常的行为。
我想要实现的是避免这种错误消息序列。实际上,我只关心第一个。
有没有办法知道我的警报视图当前是否正在显示?
类似于iOS的UIAlertView上的alert.isVisible?
2个回答

5

从您的代码来看,我怀疑通知是在后台线程触发的。在这种情况下,任何检查警报是否可见的操作都是没有帮助的。您的代码将不会启动后续的块执行,直到第一个块完成,因为 runModal 方法将以模态模式运行 NSRunLoop

要解决问题,您可以引入原子布尔属性,并在 dispatch_async 之前检查它。

Objective-C 解决方案:

- (void)operationDidFail:(NSNotification *)note {
    if (!self.alertDispatched) {
        self.alertDispatched = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
            self.alert = [NSAlert new];
            self.alert.messageText = @"Operation failed";
            [self.alert runModal];
            self.alertDispatched = NO;
        });
    }
}

使用 Swift 相同的代码:

func operationDidFail(notification: NSNotification)
{
    if !self.alertDispatched {
        self.alertDispatched = true
        dispatch_async(dispatch_get_main_queue(), {
            self.alert = NSAlert()
            self.alert.messageText = "Operation failed"
            self.alert.runModal();
            self.alertDispatched = false
        })
    }
}

1
"而不是运行模态框,您可以尝试:"
- beginSheetModalForWindow:completionHandler:

来源: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSAlert_Class/#//apple_ref/occ/instm/NSAlert/beginSheetModalForWindow:completionHandler:

在完成处理程序中将警报属性设置为nil。 仅在警报属性为nil(即每次解除警报后的第一次)时显示警报。 编辑:我没有看到文档中有任何关于您寻找的任何标志的说明。


谢谢,这个也可以。不过我还是接受了Boris的答案,因为我想继续使用runModal - Randy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接