ProjectsViewController

ProjectsViewController

我对swift中的UIKeyCommand有问题。我有两个UIViewVontrollerProjectsViewControllerViewController。我正在使用以下代码从ViewController打开ProjectsViewController

let editor = self.storyboard?.instantiateViewController(withIdentifier: "editor")
self.present(editor!, animated: true, completion: nil)

在我的课上我有一些:
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
  }
}

ProjectsViewController中我还有一个:
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
  }
}

当我的应用程序显示UIKeyCommands时,我在ipad上按了cmd键以获取可发现性,它显示了ViewController的组合,但在打开ProjectsViewController并按了cmd键后,可发现性同时显示了ProjectsViewControllerViewController的组合。如何为每个类分隔键盘快捷键?谢谢你的关注。

最佳答案

好吧,我找到了一个解决方案,也许不是最好的,但我可以确定它工作正常。
在第一个和第二个视图中,定义kind一个工厂函数,它创建您需要的所有命令

viewController1 {

func shortCutKeys() -> [UIKeyCommand] {
        return [
            UIKeyCommand(input: "1", modifierFlags: [], action: #selector(keyInput)),
           ...
            UIKeyCommand(input: "\u{8}", modifierFlags: [], action: #selector(deleteNumber(_:))),
        ]
    }

}

视图控制器2{
func shortCutKeys() -> [UIKeyCommand] {
        return [
            UIKeyCommand(input: "1", modifierFlags: [], action: #selector(keyInput)),
           ...
            UIKeyCommand(input: "\u{8}", modifierFlags: [], action: #selector(deleteNumber(_:))),
        ]
    }

}

在看的时候我好像做了那样的事
 self.shortCutKeys().forEach({
            self.addKeyCommand($0)
        })

在视界消失
self.shortCutKeys().forEach({
            self.removeKeyCommand($0)
        })

关于swift - Swift中不同 View Controller 的不同UIKeyCommand数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53457273/

10-11 00:41