提问者:小点点

#选择器(删除(_:))在uitextfield swift中崩溃


每当我在UITextField中键入任何文本并长按该文本并选择Delete时,它就会崩溃。崩溃时显示“由于未捕获异常'NSInvalidArgumentException'而终止应用程序,原因:'-[App_Name.HomeVC Delete:]:无法识别的选择器发送到实例0x14E18BA00'”。我如何解决这个崩溃问题。下面是swift代码。

    class HomeVC: UIViewController{
    @IBOutlet weak var txtSearch: UITextField!
override func viewDidLoad() {
        super.viewDidLoad()
self.txtSearch.addTarget(self, action: #selector(self.txtChanged(_:)), for: .editingChanged)
@objc func txtChanged(_ sender : UITextField){
        print(txtSearch.text!)}
}
    override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            if action == #selector(copy(_:)) || action == #selector(paste(_:)) || action == #selector(delete(_:)) || action == #selector(cut(_:))
                {
                    return true
                } else if action == Selector(("_lookup:")) || action == Selector(("_share:")) || action == Selector(("_define:")) {
                    return false
                }
                return super.canPerformAction(action, withSender: sender)
            }
    }

共1个答案

匿名用户

添加删除功能:

class HomeVC: UIViewController{
    @IBOutlet weak var txtSearch: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.txtSearch.addTarget(self, action: #selector(self.txtChanged(_:)), for: .editingChanged)
    }

    @objc func txtChanged(_ sender : UITextField){
        print(txtSearch.text!)
    }


    override func delete(_ sender: Any?) {
        txtSearch.text = ""
    }

    override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(copy(_:)) || action == #selector(paste(_:)) || action == #selector(delete(_:)) || action == #selector(cut(_:))
            {
                return true
            } else if action == Selector(("_lookup:")) || action == Selector(("_share:")) || action == Selector(("_define:")) {
                return false
            }
            return super.canPerformAction(action, withSender: sender)
        }
    }