NSAlert - 添加的按钮未按正确的顺序出现

NSAlert - Added Buttons not appearing in the correct Order

我正在尝试创建一个带有 2 个按钮的 NSAlert。

    let a = NSAlert()
    a.messageText = "Do you want go to A or B?"            
    a.alertStyle = .informational
    a.addButton(withTitle: "Yes")
    a.addButton(withTitle: "No")
    a.beginSheetModal(for: self.view.window!, completionHandler: { (modalResponse) -> Void in
    if modalResponse == NSAlertFirstButtonReturn { // do stuff}

问题是按钮 No 出现在 Yes 之前,第二个按钮似乎是 preselected.Why 是这样吗?

我需要按钮按添加顺序显示,并且没有预选按钮。

  • 通过先添加 "No" 按钮然后添加 "Yes"
  • 来修复顺序
  • 通过将 keyEquivalent 设置为“”来禁用预选

    let alert = NSAlert()
    alert.messageText = "Do you want go to A or B?"
    alert.alertStyle = .informational
    alert.addButton(withTitle: "No")
    alert.addButton(withTitle: "Yes")
    alert.buttons[0].keyEquivalent = ""
    ...