user3069232
user3069232

Reputation: 8995

Disabling some of UITextView actions on editable text

iOS 10. Swift 3

I put this into my class with the UITextView. Seemed to be the best answer for removing some of the functionality in a popup menu. Initially had a issue with it crashing and I accepted and voted the answer to that question as correct... but on further testing.. I find the code simply doesn't work as intended.

Unfortunately It infact does nothing, absolutely nothing!! beyond compiling seemly catching the menu options and than doing them anyway, even if I trying to ignore them.

It seems it used to work in objective C as far as I can tell in other SO posts, but not in Swift? Has anybody managed to get a working version in Swift that looks like this perhaps, with some subtle code change I am missing here.

override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if (action == #selector(cut)) {
        return false
    }
    return super.canPerformAction(action, withSender: sender)
}

Upvotes: 0

Views: 80

Answers (1)

rmaddy
rmaddy

Reputation: 318934

You need to reorganize your implementation a bit. It should be:

override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    if (action == #selector(delete)) {
        return false
    } else {
        return super.canPerformAction(action, withSender: sender)
    }
}

As you had it, you were ignoring the result of super.canPerformAction and always returning true. That's bad because your class doesn't respond to every single selector.

Upvotes: 1

Related Questions