Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do only two of these three addKeyCommand 's work

I have three keyboard shortcuts I am trying add to my iPad program. Here are the three

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

only the second two are recognized and show up in the screen overlay when I press the Command key on the keyboard. Also, only the second two work.

All there's actions are defined correctly. Suggestions would be appreciated.

like image 328
Michael Rowe Avatar asked Nov 28 '25 14:11

Michael Rowe


1 Answers

Ok, I am relatively new to this, but I think you are doing this wrong. Instead of what you're doing, do this.

Do all this after (outside) your 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
}

Your issue could have been anywhere from improper capitalization in your function, or syntax, or something completely different! I hope this helps you (sorry it's so late).

like image 65
owlswipe Avatar answered Dec 01 '25 04:12

owlswipe