swift - NSAlert() runmodal 此类对于键 modalWindow 不符合键值编码

标签 swift cocoa macos-catalina

最近我将我的 Mac 升级到 Catalina,并发现 NSAlert() 出现奇怪的问题。

每当打开任何警报时,我都会在控制台中收到以下错误,并且警报会自动关闭,而无需任何用户单击“确定”按钮。

控制台错误:

this class is not key value coding-compliant for the key modalWindow.' with user dictionary {
    NSTargetObjectUserInfoKey = "<ProjectName.AppDelegate: 0x100b07400>";
    NSUnknownUserInfoKey = modalWindow;
} 

下面是我用于显示警报的代码。

let myPopup: NSAlert = NSAlert()
myPopup.messageText = messageText
myPopup.informativeText = infoText
myPopup.alertStyle = NSAlert.Style.warning
myPopup.addButton(withTitle: NSLocalizedString("OK", comment: "Button Text"))
let res = myPopup.runModal()

仅供引用:这是 Mac 应用程序,使用 swift、Xcode11(尝试使用 Xcode 11.1 和 11.2)

最佳答案

终于明白了

在我的应用程序中,我们支持脚本编写,因此我在 AppDelegate 文件中添加了以下代码:

func application(_ sender: NSApplication, delegateHandlesKey key: String) -> Bool {
    return true
}

由于我始终返回 true,因此它会导致 NSOpenPanels、NSSavePanels 和 NSAlert 出现问题,并且我更改了代码,如下所示,它工作正常,没有任何问题。

func application(_ sender: NSApplication, delegateHandlesKey key: String) -> Bool {
        if key == "[.sdf file KEY HERE]"{
            return true
        }
        return false
}

请注意,我仅在最新的操作系统 Catalina 中遇到此问题,在以前的操作系统中我没有遇到任何问题。

关于swift - NSAlert() runmodal 此类对于键 modalWindow 不符合键值编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58657332/

相关文章:

cocoa - 自定义 cocoa 框架中缺少 header 文件夹

c - 在 Mac OS Catalina 中,所有当前文档均因无法找到进程 ID 的 Mach 任务端口而失败,有什么建议吗?

Macos Catalina 更新后出现错误 : unable to run colorls

objective-c - 带有尾随闭包的 Swift 到 Objective-C 调用调用了错误的方法

swift - 核心数据在存在时更新现有对象

swift - 如何访问 Parse 的 getObjectInBackgroundWithId block 之外的变量值

ios - 可取消后台任务 : NSOperation, GCD 或 NSThread?

cocoa - NSNumberFormatter 和 NSDatePicker 中的核心数据绑定(bind)相关问题

cocoa - 如何砍掉文件://from NSURL

xcode - 在 Xcode 中创建新文件时出现奇怪的相对路径。是否与 macOS Catalina beta 4 版本有关?