【问题标题】:Why do only two of these three addKeyCommand 's work为什么这三个 addKeyCommand 中只有两个起作用
【发布时间】:2015-11-29 19:20:23
【问题描述】:

我尝试将三个键盘快捷键添加到我的 iPad 程序中。这是三个

        // add the "change Status" keyboard shortcut
    let statusShortcut = UIKeyCommand(input: "s", modifierFlags: UIKeyModifierFlags.Command, action: "changeStatusPressed:", discoverabilityTitle: "Change Status of Meeting")
    addKeyCommand(statusShortcut)

    // add the "add user" keyboard shortcut
    let addShortcut = UIKeyCommand(input: "+", modifierFlags: UIKeyModifierFlags.Command, action: "addButtonPressed:", discoverabilityTitle: "Add Participant to Meeting")
    addKeyCommand(addShortcut)

    // add the "remove user" keyboard shortcut
    let removeShortcut = UIKeyCommand(input: "-", modifierFlags: UIKeyModifierFlags.Command, action: "removeButtonPressed:", discoverabilityTitle: "Remove Participant to Meeting")
    addKeyCommand(removeShortcut)

当我按下键盘上的 Command 键时,只有后两个被识别并显示在屏幕叠加层中。此外,只有后两个有效。

所有动作都被正确定义。建议将不胜感激。

【问题讨论】:

  • 为了保证没有排序问题,我把上面三个的顺序都改了,还是只能看到+和-键修饰符。
  • 我给了一个很晚的答案,希望对你有帮助!

标签: ios swift keyboard shortcut


【解决方案1】:

好的,我对此比较陌生,但我认为你做错了。而不是你正在做的事情,而是这样做。

在您的 ViewDidLoad 之后(外部)执行所有这些操作:

override func canBecomeFirstResponder() -> Bool {
return true
}

override var keyCommands: [UIKeyCommand]? {
    return [

UIKeyCommand(input: "S", modifierFlags: .Command, action: "changeStatusPressed:", discoverabilityTitle: "Change Status of Meeting"),
UIKeyCommand(input: "+", modifierFlags: .Command, action: "addButtonPressed:", discoverabilityTitle: "Add Participant to Meeting"),
UIKeyCommand(input: "-", modifierFlags: .Command, action: "removeButtonPressed:", discoverabilityTitle: "Remove Participant from Meeting"),

   ]
}

func changeStatusPressed(sender: UIKeyCommand) {
    print("command-s selected")
    // code for changeStatusPressed
}

func addButtonPressed(sender: UIKeyCommand) {
    print("command+ selected")
    // code for addButtonPressed
}

func removeButtonPressed(sender: UIKeyCommand) {
    print("command- selected")
    // code for removeButtonPressed
}

您的问题可能来自函数、语法或完全不同的大小写不正确!希望对您有所帮助(抱歉,来晚了)。

【讨论】:

  • 谢谢 没有意识到它不应该在 ViewDidLoad 中
猜你喜欢
  • 1970-01-01
  • 2020-11-28
  • 2013-09-17
  • 2020-06-24
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
相关资源
最近更新 更多