gpt4 book ai didi

objective-c - 当用户在 NSAlert 后按 Enter 时未设置 NSIndexSet

转载 作者:行者123 更新时间:2023-12-03 17:47:57 27 4
gpt4 key购买 nike

我有一个非常简单的 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。

有人能帮忙解决一下吗?谢谢

最佳答案

所以,我基本上需要添加一个相当于 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) {

}

关于objective-c - 当用户在 NSAlert 后按 Enter 时未设置 NSIndexSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43312878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com