我有一个非常简单的Mac应用程序,在某个特定时间,一个按钮会显示带有输入字段的NSAlert。之后,我在NSTableView上采取了一些操作。 如果用户按下OK按钮,它可以正常工作,但如果用户按下ENTER,则不会定义选定的
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。
有人可以帮忙解决吗? TKS
答案 0 :(得分:1)
所以,我基本上需要添加一个等同于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) {
}