user3069232 user3069232 - 7 days ago 6
Swift Question

Disabling UITextView Delete actions on editable text

iOS 10. Swift 3

I put this into my class with the UITextView. It disables delete on editable text, but leaves the others working. But it only works once. If you press select, for example, it's good. But if you then press something else, select all for example, it crashes badly with the error message:


unrecognized selector sent to instance 0x103121800


Is this a fluke? Should it work at all? Can I get around this nasty crashing behavior?

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

Answer

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.

Comments