How to disable pasting in a TextField in Swift?

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

    I did use this code. This is working.

    override func canPerformAction(_ action: Selector, withSender sender: Any?)  Bool {
    
        if YOURTEXTFIELD.isFirstResponder {
            DispatchQueue.main.async(execute: {
                (sender as? UIMenuController)?.setMenuVisible(false, animated: false)
            })
            return false
        }
    
        return super.canPerformAction(action, withSender: sender)
    }
    

提交回复
热议问题