Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resignFirstResponder in Swift

Tags:

swift

ios8

uiview

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()
}
like image 474
Alexey Nakhimov Avatar asked Jun 04 '14 10:06

Alexey Nakhimov


People also ask

What is Endediting in Swift?

Causes the view (or one of its embedded text fields) to resign the first responder status.

What is a UIResponder?

An abstract interface for responding to and handling events.

How do I hide the keyboard in Objective C?

In Objective-C:[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; This will resign the first responder (and dismiss the keyboard) every time, without you needing to send resignFirstResponder to the proper view. No matter what, this will dismiss the keyboard.


2 Answers

You could probably go with:

self.view.endEditing(true)
like image 70
Rui Peres Avatar answered Sep 20 '22 18:09

Rui Peres


Try this

self.view.endEditing(true)
like image 34
Rajneesh071 Avatar answered Sep 23 '22 18:09

Rajneesh071