I need to configure my UITextView such that it only performs a select set of actions via the Edit menu. I tried to override the 'canPerformAction(_ action: Selector, withSender sender: Any?)' function to achieve this.
class NewTextView: UITextView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(paste(_:)) || action == #selector(copy(_:)) || action == #selector(cut(_:)) || action == #selector(select(_:)) || action == #selector(selectAll(_:))
{
return super.canPerformAction(action, withSender: sender)
}
return false
}
}
But when I tried to check for the 'replace(_:)' selector, I got the following error. which says "Cannot find 'replace' in scope". error screenshot.
Right now I have handled it by checking for its description, but I'm not sure how robust this method is.
if action.description == "replace:"
{
return true
}
There is no
replaceselector in the Standard Edit Actions so it cannot work anyway.The
action.descriptioncheck is fine but if you want to check the selector directly outside of a class conforming toUIViewyou have to specify the type where the selector is declared, for examplepaste