当用户在 NSAlert 之后按回车键时 NSIndexSet 未设置

NSIndexSet not set when user press enter after NSAlert

我有一个非常简单的 Mac 应用程序,在某个特定时间,一个按钮会弹出一个带有输入字段的 NSAlert。之后,我对 NSTableView 进行了一些操作。 如果用户按下 OK 按钮,它工作正常,但如果用户按下 ENTER,它不会定义 selectedrows

 NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Damage"];
[alert addButtonWithTitle:@"Ok"];
[alert addButtonWithTitle:@"Cancel"];
NSInteger damage = 0;
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[input setStringValue:@""];
[[alert window]setInitialFirstResponder:input];

[alert setAccessoryView:input];
NSInteger button = [alert runModal];
if (button == NSAlertFirstButtonReturn) {
     damage = [input integerValue];
} else if (button == NSAlertSecondButtonReturn) {

}

//data
NSIndexSet *set = [self.tableView selectedRowIndexes];

NSUInteger index = [set lastIndex];

索引的值为 NSNotFound。

有人可以帮忙解决吗? 谢谢

所以,我基本上需要添加一个相当于 ENTER 的键,它解决了问题:

 NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Damage/Heal"];
[alert addButtonWithTitle:@"Ok"];
[alert addButtonWithTitle:@"Cancel"];
NSInteger damage = 0;
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
[input setStringValue:@""];
[[alert window]setInitialFirstResponder:input];

[alert setAccessoryView:input];
alert.buttons[0].keyEquivalent=@"\r";//new code
NSInteger button = [alert runModal];
if (button == NSAlertFirstButtonReturn) {
    damage = [input integerValue];
} else if (button == NSAlertSecondButtonReturn) {

}