I would like to use resign the first responder on all uitextfield
. I'm able to complete this by placing the uitextfields
in an array but I wanted to avoid using the array. the resign should happen to all type of uiTextField how can this be done.
This works fine
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var text: UITextField!
@IBOutlet weak var textFieldtwo: UITextField!
var textField = [UITextField]()
override func viewDidLoad() {
super.viewDidLoad()
self.textField = [text,textFieldtwo]
for item in textField {
item.delegate = self
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("text")
for item in textField {
item.resignFirstResponder()
}
}
}
Approach #1: Using the Responder Chain [[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.
resignFirstResponder() Notifies this object that it has been asked to relinquish its status as first responder in its window.
becomeFirstResponder() Notifies the receiver that it's about to become first responder in its NSWindow .
The first responder is whatever control is currently ready to respond to actions. In UIKit this is usually the control that has activated the keyboard and is receiving input.
I have not seen anything in the documentation suggesting SwiftUI 2 supports setting the first responder unfortunately. No, this is not supported in a general way, though watchOS and tvOS have new view modifiers for influencing which focusable view receives focus by default.
If you need to take text input in your Swift app, you will probably need a UITextField. It is exactly what it sounds like, just a field on the screen where the user types something in.
I just tested it with a textfield by calling self.tf.becomeFirstResponder () indside viewDidLoad () function and its working absolutely fine. You'll need to toggle your keyboard by pressing command + K as nflacco just pointed out and disable the hardware keyboard in the simulator as:
Then, usually, when the user clicks outside of the UITextField, the keyboard is dismissed, and the cursor is no longer in that UITextField. However, a common issue for many iOS programming beginners is that the last part of that story isn’t built in.
Or just use a more global approach. (Swift 3.0)
UIApplication.shared.sendAction(#selector(UIApplication.resignFirstResponder), to: nil, from: nil, for: nil);
This will dismiss any active field as it resigns the current first responder. Good luck coding!
You can try this:
for textField in self.view.subviews where textField is UITextField { textField.resignFirstResponder() }
But if you just want to dismiss the Keyboard by pressing the return
on the keyboard
or tapping anywhere on the screen without using touchesBegan
You can try with this:
// For pressing return on the keyboard to dismiss keyboard func textFieldShouldReturn(textField: UITextField) -> Bool { for textField in self.view.subviews where textField is UITextField { textField.resignFirstResponder() } return true }
...
func hideKeyboard() { view.endEditing(true) }
And add this to your viewDidLoad
:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(hideKeyboard)) view.addGestureRecognizer(tapGesture)
A simpler approach would by to end editing on the UIView containing the UITextFields by saying:
view.endEditing(true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With