How to disable pasting in a TextField in Swift?

后端 未结 14 1478
予麋鹿
予麋鹿 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:06

    You can create an extension for UITextField and override canPerformAction:

    override public func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
            return (action != "paste:") 
    }
    

提交回复
热议问题