【问题标题】:touchesBegan does not firetouchesBegan 不触发
【发布时间】:2014-07-20 22:02:33
【问题描述】:

当用户点击视图时,我有以下代码隐藏我的键盘,但 touchesBegan 根本没有触发:

class LoginViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var emailAddress: UITextField

    @IBOutlet var password: UITextField



    override func viewDidLoad() {

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //Delegate fields
        self.emailAddress.delegate = self
        self.password.delegate = self


    }

     override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
        self.emailAddress.resignFirstResponder()
        self.password.resignFirstResponder()
    }

    func textFieldShouldReturn(textField: UITextField!) -> Bool{
        self.emailAddress.resignFirstResponder()
        self.password.resignFirstResponder()
        return true;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

此视图控制器位于导航控制器内部,因此不确定它是否与响应者链有关

【问题讨论】:

    标签: ios swift touch-event


    【解决方案1】:

    您的代码对我来说很好用。 (不过,通常我会在每个文本字段上使用 self.view.endEditing(YES) 而不是 resignFirstResponder。)

    您正在点击的视图很可能会以某种方式阻止事件向上发送到响应者链。它可能是 userInteractionEnabled,alpha 为 0,覆盖 touchesBegan,它不会将事件发送到响应者链上,一个手势识别器正在吃触摸事件等。如果你做一个显示这个问题的最小测试用例,很可能会很明显它是哪一个。

    【讨论】:

    • 嗯,我检查了 userinteractionenabled,alpha 为 1,我的代码中没有其他覆盖,也没有手势识别器。我怎样才能为此做一个测试用例?
    • 我在控制台中也得到了一些东西:“Interface Builder 文件中的未知类 LoginViewController。”
    • @anthonypliu 这几乎肯定是问题所在。我建议搜索该问题。例如:stackoverflow.com/questions/24020610/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多