Home » Swift » resignFirstResponder in Swift

resignFirstResponder in Swift

Posted by: admin November 30, 2017 Leave a comment

Questions:

How can I implement it in the new language of Apple:

Objective-C code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        for (UIView *view in self.view.subviews)
        [view resignFirstResponder];
}

I have tried to do so. But the keyboard does not disappear:

Swift code:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)
    self.view.resignFirstResponder()
}
Answers:

You could probably go with:

self.view.endEditing(true)

Questions:
Answers:

Try this

self.view.endEditing(true)