I have a problem with UIKeyCommand
in Swift. I have two UIViewVontroller
, ProjectsViewController
and ViewController
. I'm opening ViewController
from ProjectsViewController
using this code:
let editor = self.storyboard?.instantiateViewController(withIdentifier: "editor")
self.present(editor!, animated: true, completion: nil)
In my ProjectsViewController
class I have some 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
}
}
And in ViewController
I have another:
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
}
}
When my app presents ProjectsViewController
, I've pressed cmd for Discoverability on my iPad, and it presents combinations for ProjectsViewController
, but after I've opened ViewController
and press cmd, Discoverability shows both combinations for ProjectsViewController
and ViewController
. How can I separate keyboard shortcuts for every class? Thank you for your attention.
Ok I found a solution and maybe its not the best but I can be sure it works properly.
on the first and second views, define kind a factory function that create all commands you need like that
viewController2 {
at viewDidAppear do something like that
at viewDidDisappear