Objective c 如何制作NSAlert&x27;第二个按钮是返回按钮吗?

Objective c 如何制作NSAlert&x27;第二个按钮是返回按钮吗?,objective-c,macos,cocoa,nsalert,Objective C,Macos,Cocoa,Nsalert,我想做这样的东西NSAlert: 如您所见,“返回”按钮是第二个按钮。我该怎么做 下面是我用来创建我的NSAlert的代码示例,但第一个按钮获得了焦点: NSAlert *alert = [[NSAlert alloc] init]; [alert setMessageText:@"Are you sure you want to disconnect?"]; [alert addButtonWithTitle:@"Disconnect"]; [alert addButtonWithTitle:

我想做这样的东西
NSAlert

如您所见,“返回”按钮是第二个按钮。我该怎么做
下面是我用来创建我的
NSAlert
的代码示例,但第一个按钮获得了焦点:

NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Are you sure you want to disconnect?"];
[alert addButtonWithTitle:@"Disconnect"];
[alert addButtonWithTitle:@"Cancel"];
[alert runModal];

我想聚焦“取消”按钮。有什么想法吗?谢谢

要更改
NSAlert
对象内
NSButton
元素的密钥等价物,您必须直接访问按钮(在创建之后和
-runModal
之前),并使用
-setKeyEquivalent:
方法更改密钥等价物

例如,要将
Disconnect
设置为ESC,将
Cancel
设置为return,您可以执行以下操作:

NSArray *buttons = [alert buttons];
// note: rightmost button is index 0
[[buttons objectAtIndex:1] setKeyEquivalent: @"\033"];
[[buttons objectAtIndex:0] setKeyEquivalent:@"\r"];

在调用
-runModal
Swift 4之前

let alert = NSAlert()
alert.messageText = question
alert.informativeText = text
alert.alertStyle = .critical
let deleteButton = alert.addButton(withTitle: "Delete")
let cancelButton = alert.addButton(withTitle: "Cancel")
deleteButton.keyEquivalent = ""
cancelButton.keyEquivalent = "\r"

您可能对这个cocoa-dev线程感兴趣:最右边的按钮(“断开”)位于索引0处。其键为“\r”,即返回,而不是ESC。我可能弄错了?