【发布时间】:2018-11-24 10:36:19
【问题描述】:
我对 Swift 中的 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 以显示可发现性,它显示了 ProjectsViewController 的组合,但在我打开 ViewController 并按 cmd 后,可发现性显示 @ 的两种组合987654336@ 和ViewController。如何为每个班级分开键盘快捷键?感谢您的关注。
【问题讨论】:
-
这与我遇到的问题完全相同。我正在尝试停用诸如 override func viewDidDisappear(_ animated: Bool) { NotificationCenter.default.removeObserver(self) self.keyCommands?.forEach({ log.debug($0) self.removeKeyCommand($0) }) } 即使它根本不起作用。
标签: swift ipad keyboard uikit uikeycommand