gpt4 book ai didi

objective-c - 如何使用 UIKeyCommand

转载 作者:行者123 更新时间:2023-12-04 02:09:17 26 4
gpt4 key购买 nike

当用户按下硬件键盘上的向下箭头时,我试图在表 View 中选择一行。现在,我只是想打印一个日志,但似乎无法让它工作。根据其他类似问题的答案,这是我目前所拥有的:

- (NSArray *)keyCommands {
UIKeyCommand *downArrowKeyCommand = [UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow
modifierFlags:0
action:@selector(hardwareKeyboardDownArrowPressed)];

return @[downArrowKeyCommand];
}

- (BOOL)canBecomeFirstResponder {
return YES;
}

- (void)hardwareKeyboardDownArrowPressed {

NSLog(@"Down arrow pressed on external keyboard");

}

感谢所有帮助!

最佳答案

我不知道你的具体错误;可能是您没有添加 (id)sender到你的方法。我想出了如何做到这一点。我将在下面添加我的代码:

- (BOOL)canBecomeFirstResponder {
return YES;
}

- (NSArray *)keyCommands {
return @[[UIKeyCommand keyCommandWithInput:UIKeyInputDownArrow modifierFlags:0 action:@selector(moveDownOneRow:) discoverabilityTitle:@"Select row down"],
[UIKeyCommand keyCommandWithInput:UIKeyInputUpArrow modifierFlags:0 action:@selector(moveUpOneRow:) discoverabilityTitle:@"Select row up"]];
}

- (void)moveDownOneRow:(id)sender {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

if (indexPath == nil) {
indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
} else {
if ((indexPath.row + 1) < [self.tableView numberOfRowsInSection:0]) {
indexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
}
}

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}

- (void)moveUpOneRow:(id)sender {
NSIndexPath *indexPath = [self.tableViewindexPathForSelectedRow];

if (indexPath == nil) {
indexPath = [NSIndexPath indexPathForRow:([self.tableView numberOfRowsInSection:0]-1) inSection:0];
} else {
if ((indexPath.row - 1) >= 0) {
indexPath = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:0];
}
}

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}

关于objective-c - 如何使用 UIKeyCommand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40263960/

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