gpt4 book ai didi

swift - Swift 中不同 View Controller 的不同 UIKeyCommand 数组

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:32 24 4
gpt4 key购买 nike

我在 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,Discoverability 会显示 ProjectsViewControllerViewController 的组合。如何为每个类(class)分开键盘快捷键?感谢您的关注。

最佳答案

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

在第一个和第二个 View 中,定义一个工厂函数来创建你需要的所有命令

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)
})

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com