Sundara Aadithyan
Sundara Aadithyan

Reputation: 11

canPerformAction(_ action: Selector, withSender sender: Any?) function in UITextView does not recognize 'replace(_:)' action

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
}

Upvotes: 0

Views: 490

Answers (1)

vadian
vadian

Reputation: 285059

There is no replace selector in the Standard Edit Actions so it cannot work anyway.

The action.description check is fine but if you want to check the selector directly outside of a class conforming to UIView you have to specify the type where the selector is declared, for example paste

if action == #selector(UIResponder.paste(_:))

Upvotes: 0

Related Questions