【问题标题】:Different UIKeyCommand arrays for different view controllers in SwiftSwift 中不同视图控制器的不同 UIKeyCommand 数组
【发布时间】:2018-11-24 10:36:19
【问题描述】:

我对 Swift 中的 UIKeyCommand 有疑问。我有两个UIViewVontrollerProjectsViewControllerViewController。我正在使用此代码从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


【解决方案1】:

好的,我找到了一个解决方案,也许它不是最好的,但我可以确定它可以正常工作。

在第一个和第二个视图上,定义一种工厂函数,以创建您需要的所有命令

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

}

在 viewDidAppear 做类似的事情

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

在 viewDidDisappear

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多