2

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.

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
  • this is exactly the same problem I have. I'm trying to deactivate the previews controller commands like that override func viewDidDisappear(_ animated: Bool) { NotificationCenter.default.removeObserver(self) self.keyCommands?.forEach({ log.debug($0) self.removeKeyCommand($0) }) } even though, it doesn't work at all. – JBarros35 Oct 09 '19 at 13:12

1 Answers1

1

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)
        })
JBarros35
  • 976
  • 1
  • 12
  • 18