Swift中不同视图控制器的不同UIKeyCommand数组



Swift中的UIKeyCommand有问题。我有两个UIViewVontrollerProjectsViewControllerViewController。我使用以下代码从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,它显示了ProjectsViewController的组合,但在我打开ViewController并按cmd后,可发现性显示了ProjectsViewControllerViewController的组合。如何将每个类的键盘快捷键分开?感谢您的关注。

好的,我找到了一个解决方案,也许不是最好的,但我可以确保它正常工作。

在第一个和第二个视图中,定义一种工厂函数,它可以创建您需要的所有命令,比如

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(_:))),
]
}
}

在视图DidPear做一些类似的事情

self.shortCutKeys().forEach({
self.addKeyCommand($0)
})

查看DidDisappear

self.shortCutKeys().forEach({
self.removeKeyCommand($0)
})

相关内容

  • 没有找到相关文章

最新更新