Key commands use the target-action pattern, meaning they look at whether something in the responder chain implements the action method for the command.
For example, the action method for the Copy command is copy:
:
https://developer.apple.com/documentation/uikit/uiresponderstandardeditactions/2354191-copy
So as long as anything in the responder chain implements a copy:
method with the same method signature, then the copy command will be available (and it will call that method when the key command is triggered). All UIKit text editing views, such as UITextField and UITextView, already implement the standard edit methods like copy:
, paste:
, cut:
, and so on, so if the user is editing text, then these commands will work automatically for text editing. However, you can still implement the methods in your own views to get them to work there as well.
You can also have more fine-grained control over when the copy command is available by implementing the canPerformAction:withSender:
method (in the same object that also implements the copy:
method). For example, you can return NO from this method for the copy:
action, if there's no content to copy. That will suppress the Copy command from working or appearing in the keyboard shortcut menu if there's nothing to copy. Do ensure you call super for unhandled cases in that method, though.