How to disable pasting in a TextField in Swift?

后端 未结 14 1449
予麋鹿
予麋鹿 2020-11-30 03:41

I\'ve got a TextField with a numberPad and the function runs only if it contains numbers.

The user will crash the app if they paste letters

14条回答
  •  时光取名叫无心
    2020-11-30 04:13

    For Swift 3 it's changed to:

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

提交回复
热议问题