NSAlert的破坏性按钮

4
随着 Big Sur 登陆我们的 Mac,我注意到 NSAlert 控制器中破坏性按钮的文本被标成了红色,如图所示。

An example from the Apple's Message app

我无法找到一种方法来在我的应用程序中添加此功能。
使用标准的addButton(withTitle:)方法,我们没有任何方法来设置其意图(如默认、取消或破坏性)。
你能给我一些提示吗?
谢谢。
2个回答

7

谢谢。我看了那个文件,但可能太快了,我真的错过了。再次感谢! - undefined

3

vadian 的回答 中补充一点:

似乎只有当按钮不是第一个(因此不是蓝色高亮按钮)时,将按钮设置为破坏性才有效。 对于所有好奇的人,以下是代码示例:

let alert = NSAlert()
alert.messageText = "Alert Title"
alert.alertStyle = .warning
alert.addButton(withTitle: "Cancel")
alert.addButton(withTitle: "Destructive Button")
    
if #available(macOS 11.0, *) {
    alert.buttons.last?.hasDestructiveAction = true
}

缺点是用户不能通过回车键接受破坏性操作,只能手动点击或通过选项卡选择按钮(必须在设置中启用此选项)。


1
用户不能通过按下回车键来接受破坏性操作。 - undefined

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