我试图添加键盘快捷键到我的应用程序,我有一个问题的行动,UIKeyCommand没有被调用。
我有一个UIViewController,它正在覆盖KeyCommands。
- (BOOL)becomeFirstResponder
{
return YES;
}
- (NSArray<UIKeyCommand *> *)keyCommands
{
return self.keyCommandManager.keyShortcutsArray;
}我还有一个KeyCommandManager类NSObject,它有两种方法,一种是根据应用程序的状态设置keyShortcutsArray,另一种是UIKeyCommands应该是tigger的方法。
- (void)setKeyShortcutsOfType:(ShortcutType)shortcutType
{
switch(shortcutType)
{
case PlaybackPreviewShortcut:
self.keyShortcutsArray = @[[UIKeyCommand keyCommandWithInput:@" " modifierFlags:0 action:@selector(keyShortcutActions:) discoverabilityTitle:@"Toggle playback preview"]];
break;
default:
self.keyShortcutsArray = @[];
break;
}
- (void)keyShortcutActions:(UIKeyCommand)sender
{
NSLog(@"#### This method is not being called by the space key shortcut");
}当前,当按下键时,KeyCommand重写方法将得到正确的数组。但是,这些键的选择器不起作用,也没有调用keyShortcutActions方法。
发布于 2019-03-13 16:50:25
来自苹果的文档
从此方法返回的键命令将应用于整个响应链。当按下与key命令对象匹配的键组合时,UIKit在响应链中寻找实现相应操作方法的对象。它对它找到的第一个对象调用该方法,然后停止处理该事件。
您的keyCommandManger实例NSObject不在响应链中--视图控制器是。
如果你把这个方法:
- (void)keyShortcutActions:(UIKeyCommand)sender
{
NSLog(@"#### This method IS being called (in view controller) by the space key shortcut");
}你应该看到它被触发了。
如果希望将“操作”代码包含在keyCommandManger中,则可以将事件转发给manager对象。或者,您可以尝试将您的manager类更改为从UIResponder继承--但可靠地将其放入链中是很困难的部分。
https://stackoverflow.com/questions/55144397
复制相似问题