Different UIKeyCommand arrays for different view controllers in Swift

412 views Asked by At

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.

1

There are 1 answers

0
JBarros35 On

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

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

}

at viewDidAppear do something like that

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

at viewDidDisappear

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