【发布时间】:2019-04-22 17:27:10
【问题描述】:
我目前正试图阻止所有UIResponderStandardEditActions(如copy、paste、delete)在UITextfield 为空时出现。我只想在用户输入消息时向他们展示。我已经尝试了 2 种解决方案,但目前不起作用,我不确定它是否与 iOS 12 或有关。我在UITextfield extension 中尝试了overriding 和canPerformAction 方法,并在Storyboard 中使用了后来分配给UITextfield 的自定义类,但没有运气。有没有另一种方法可以做到这一点。这是我尝试过的。
extension UITextField {
open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if self.text!.isEmpty {
return false
}
return action == #selector(UIResponderStandardEditActions.paste(_:))
}
}
class CustomTextField: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.paste(_:)) || action == #selector(UIResponderStandardEditActions.copy(_:)) || action == #selector(UIResponderStandardEditActions.delete(_:)) {
return false
}
return true
}
}
【问题讨论】:
标签: ios swift uitextfield uiresponder