For the More Curious: UIMenuController and UIResponderStandardEditActions

The UIMenuController is typically responsible for showing the user an edit menu when it is displayed. (Think of a text field or text view when you press and hold.) Therefore, an unmodified menu controller (one that you do not set the menu items for) already has default menu items that it presents, like Cut, Copy, and other familiar options. Each item has an action message wired up. For example, cut: is sent to the view presenting the menu controller when the Cut menu item is tapped.

All instances of UIResponder implement these methods, but, by default, these methods do not do anything. Subclasses like UITextField override these methods to do something appropriate for their context, like cut the currently selected text. The methods are all declared in the UIResponderStandardEditActions protocol.

If you override a method from UIResponderStandardEditActions in a view, its menu item will automatically appear in any menu you show for that view. This works because the menu controller calls the method canPerformAction(_:withSender:) on its view, which returns true or false depending on whether the view implements this method.

If you want to implement one of these methods but do not want it to appear in the menu, you can override canPerformAction(_:withSender:) to return false:

override func canPerformAction(_ action: Selector,
        withSender sender: Any?) -> Bool {

    if action == #selector(copy(_:)) {
        return false
    } else {
        // Else return the default behavior
        return super.canPerformAction(action, withSender: sender)
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset