Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift how to resign first responder on all uiTextfield

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()
            } 
        }
    }
like image 226
SwiftER Avatar asked May 14 '16 16:05

SwiftER


People also ask

How do I resign from first responder?

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.

What does resignFirstResponder do?

resignFirstResponder() Notifies this object that it has been asked to relinquish its status as first responder in its window.

What is becomeFirstResponder in IOS?

becomeFirstResponder() Notifies the receiver that it's about to become first responder in its NSWindow .

What is first responder Swift?

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.

Does SwiftUI 2 support setting the first responder?

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.

What is uitextfield in Swift?

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.

How to make a textfield become First Responder?

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:

What happens when you click outside of a uitextfield?

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.


3 Answers

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!

like image 101
Hjalmar Avatar answered Oct 08 '22 19:10

Hjalmar


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) 
like image 30
Khuong Avatar answered Oct 08 '22 17:10

Khuong


A simpler approach would by to end editing on the UIView containing the UITextFields by saying:

view.endEditing(true)
like image 42
Ahmed Onawale Avatar answered Oct 08 '22 18:10

Ahmed Onawale