我和斯威夫特的UIKeyCommand有点问题。我有两个UIViewVontroller,ProjectsViewController和ViewController。我使用以下代码从ProjectsViewController打开ViewController:
let editor = self.storyboard?.instantiateViewController(withIdentifier: "editor")
self.present(editor!, animated: true, completion: nil)在我的ProjectsViewController类中,我有一些UIKeyCommands
override var keyCommands: [UIKeyCommand]?
{
if #available(iOS 9.0, *)
{
return [
UIKeyCommand(input: "n", modifierFlags: .command, action: #selector(projectWizard), discoverabilityTitle: "Create new project"), UIKeyCommand(input: "t", modifierFlags: .command, action: #selector(toolsAction), discoverabilityTitle: "Open Tools"), UIKeyCommand(input: ",", modifierFlags: .command, action: #selector(settingsAction), discoverabilityTitle: "Open Settings"), UIKeyCommand(input: "i", modifierFlags: .command, action: #selector(aboutAction), discoverabilityTitle: "About")
]
}
else
{
return nil
}
}在ViewController中,我有另一个:
override var keyCommands: [UIKeyCommand]?
{
if #available(iOS 9.0, *)
{
return [
UIKeyCommand(input: "n", modifierFlags: .command, action: #selector(addFileAction), discoverabilityTitle: "New file"), UIKeyCommand(input: "r", modifierFlags: .command, action: #selector(runProject), discoverabilityTitle: "Build and run"), UIKeyCommand(input: "w", modifierFlags: .command, action: #selector(closeProject), discoverabilityTitle: "Close window")
]
}
else
{
return nil
}
}当我的应用程序显示ProjectsViewController时,我在iPad上按了cmd for Discoverability,它显示了ProjectsViewController的组合,但在我打开ViewController并按cmd后,Discoverability显示了ProjectsViewController和ViewController的组合。如何为每个班级分别使用键盘快捷键?感谢您的关注。
发布于 2019-10-09 21:49:17
好吧,我找到了一个解决方案,也许它不是最好的,但我可以肯定它工作正常。
在第一个和第二个视图上,定义一个工厂函数来创建所需的所有命令,如下所示
viewController1 {
func shortCutKeys() -> [UIKeyCommand] {
return [
UIKeyCommand(input: "1", modifierFlags: [], action: #selector(keyInput)),
...
UIKeyCommand(input: "\u{8}", modifierFlags: [], action: #selector(deleteNumber(_:))),
]
}
}viewController2 {
func shortCutKeys() -> [UIKeyCommand] {
return [
UIKeyCommand(input: "1", modifierFlags: [], action: #selector(keyInput)),
...
UIKeyCommand(input: "\u{8}", modifierFlags: [], action: #selector(deleteNumber(_:))),
]
}
}在viewDidAppear做这样的事情
self.shortCutKeys().forEach({
self.addKeyCommand($0)
})在viewDidDisappear
self.shortCutKeys().forEach({
self.removeKeyCommand($0)
})https://stackoverflow.com/questions/53457273
复制相似问题