Switching between Text fields on pressing return key in Swift

uitextfield automatically move to next field swift
how to move cursor from one text field to another automatically in swift ios programmatically?
uitextfield return key swift
textview should return swift
swift keyboard return key action
swiftui textfield return key
uitextview return key
dismiss keyboard swift

I'm designing an iOS app and I want that when the return key is pressed in my iPhone it directs me to the next following text field.

I have found a couple of similar questions, with excellent answers around but they all just happen to be in Objective-C and I'm looking for Swift code, now this is what I have up until now:

func textFieldShouldReturn(emaillabel: UITextField) -> Bool{
    return true
}

It's placed in the file that's connected and controller to the UIView that contains the text fields, but I'm not sure if thats the right place.

I'm basically new to Swift, so explain every little step or I'll manage to mess it up somehow. Also I'm using the latest version of Xcode if that makes any difference.

Okay, so I tried this out and got this error: //could not find an overload for '!=' that accepts the supplied arguments

func textFieldShouldReturn(textField: UITextField) -> Bool {
    let nextTag: NSInteger = textField.tag + 1
    // Try to find next responder
    let nextResponder: UIResponder = textField.superview!.viewWithTag(nextTag)!
    if (nextResponder != nil) {
        // could not find an overload for '!=' that accepts the supplied arguments

        // Found next responder, so set it.
        nextResponder.becomeFirstResponder()
    } else {
        // Not found, so remove keyboard.
        textField.resignFirstResponder()
    }
    return false // We do not want UITextField to insert line-breaks.
}

Thanks in advance pals!

Make sure your UITextField delegates are set and the tags are incremented properly. This can also be done through the Interface Builder.

Here's a link to an Obj-C post I found: How to navigate through textfields (Next / Done Buttons)

class ViewController: UIViewController,UITextFieldDelegate {
   // Link each UITextField (Not necessary if delegate and tag are set in Interface Builder)
   @IBOutlet weak var someTextField: UITextField!

   override func viewDidLoad() {
      super.viewDidLoad()
      // Do the next two lines for each UITextField here or in the Interface Builder
      someTextField.delegate = self
      someTextField.tag = 0 //Increment accordingly
   }

   func textFieldShouldReturn(_ textField: UITextField) -> Bool {
      // Try to find next responder
      if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
         nextField.becomeFirstResponder()
      } else {
         // Not found, so remove keyboard.
         textField.resignFirstResponder()
      }
      // Do not add a line break
      return false
   }
}

How to move to the next UITextField when the user presses return , If you're in a situation where your iOS app has multiple UITextField instances able to move between them by pressing Next/Return on their on-screen keyboard​. The easiest approach is using view tags: give your text fields  Swift 5. You can easily switch to another TextField when clicking return key in keyboard. First, Your view controller conforms to UITextFieldDelegate and add the textFieldShouldReturn(_:) delegate method in ViewController; Drag from TextField to ViewController in Interface Builder. Then select the delegate option. Note: Do this for all TextField

iPhone Apps 101, The next part is to set the delegate on all of our text fields. func textFieldShouldReturn(_ textField: UITextField) -> Bool { return true } In the first part we will use a switch statement to move to our next textfield. textfield you would need to use a keyboard input accessory view or some other form of control​. all of the text fields are subviews of the same view. the text fields have tags in the order you want to progress (e.g., textField2.tag = 2, textField3.tag = 3, etc.) moving to the next text field will happen when you tap the return button on the keyboard (you can change this to next, done, etc.)

I suggest that you should use switch statement in textFieldShouldReturn(_:).

// MARK: UITextFieldDelegate

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    switch textField {
    case nameTextField:
        phoneTextField.becomeFirstResponder()
    case phoneTextField:
        emailTextField.becomeFirstResponder()
    case emailTextField:
        descriptionTextField.becomeFirstResponder()
    default:
        textField.resignFirstResponder()
    }
    return false
}

UITextField Return key: Swift Sidebars, UITextField form with Keyboard Input Accessory View UIToolbar for Previous/​Next Buttons set all of the UITextFields' delegate to the view controller (self) When it finds one, it sets the delegate the self and sets the keyboard return to '​Next' (it stores the last text field – designated by ViewController.swift. Make sure your text fields have their delegate set and implement the textFieldShouldReturn method. This is the method that is called when the user taps the return key (no matter what it looks like). The method might look something like this:

This approach needs some changes in table views and collection views, but it's okay for simple forms I guess.

Connect your textFields to one IBOutletCollection, sort it by its y coordinate and in textFieldShouldReturn(_:) just jump to the next textfield until you reach the end:

@IBOutlet var textFields: [UITextField]!

...

textFields.sortInPlace { $0.frame.origin.y < $1.frame.origin.y }

...

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if let currentIndex = textFields.indexOf(textField) where currentIndex < textFields.count-1 {
        textFields[currentIndex+1].becomeFirstResponder()
    } else {
        textField.resignFirstResponder()
    }
    return true
}    

Or just look at sample project (xcode 7 beta 4)

Move to next text field with Swift, UITextField after pressing the keyboard Return button using Swift. as I think most of the cases will have multiple text fields loaded inside a  Set UITextField Keyboard Return Key as Done button And Dismiss Keyboard. In this short Swift code example we will learn how to create UITextField in Swift programmatically as well as how to set Keyboard return button as Done button and how to handle Done button to dismiss keyboard. Create UITextField of specific height and width.

I have tried many codes and finally this worked for me in Swift 3.0 Latest [March 2017]

The "ViewController" class should inherited the "UITextFieldDelegate" for making this code working.

class ViewController: UIViewController,UITextFieldDelegate  

Add the Text field with the Proper Tag nuber and this tag number is used to take the control to appropriate text field based on incremental tag number assigned to it.

override func viewDidLoad() {

 userNameTextField.delegate = self

        userNameTextField.tag = 0

        userNameTextField.returnKeyType = UIReturnKeyType.next

        passwordTextField.delegate = self

        passwordTextField.tag = 1


        passwordTextField.returnKeyType = UIReturnKeyType.go

}

In the above code, the "returnKeyType = UIReturnKeyType.next" where will make the Key pad return key to display as "Next" you also have other options as "Join/Go" etc, based on your application change the values.

This "textFieldShouldReturn" is a method of UITextFieldDelegate controlled and here we have next field selection based on the Tag value incrementation

func textFieldShouldReturn(_ textField: UITextField) -> Bool

    {

        if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {

            nextField.becomeFirstResponder()

        } else {

            textField.resignFirstResponder()

            return true;

        }

        return false

    }

UITextField Form with Previous/Next Keyboard Accessory Input View, A protip by flexicoder about ios, ipad, iphone, and objective-c. Next on the keyboard you need to resignFirstResponder from the current field and You need to set the Return Key types accordingly within the interface builder. and the textfields delegate assignment must be set via IB to the File's Owner. With SwiftKey you have the option to choose between having a default enter (return) or emoji key. To edit this setting: Open the SwiftKey app from your device. Tap 'Typing'. Tap 'Keys'. Check/uncheck ‘Dedicated emoji key’. When this option is left unchecked, emoji can still be accessed from within your messaging apps by long pressing the enter key.

Move to the next UITextField by hitting Return - Swift2Go, Asks the delegate if the text field should process the pressing of the return button. SDKs. iOS 2.0+; Mac Catalyst 13.0+; tvOS 9.0+. In the settings area for WhatsApp you can adjust the behavior for the return/enter key. The below steps will likely resolve this for you: 1 - Go into WhatsApp settings. 3 - Uncheck the box at the top that says 'Enter is send'

Moving to the next UITextField in an iOS App (Example), Switching between Text fields on pressing return key in Swift. I'm designing an iOS app and I want that when the return key is pressed in my iPhone it directs me​  Stay in the same cell after pressing the Enter key with Shortcut Keys. In Excel, you can also use shortcut keys to solve this task. After entering the content, please press Ctrl + Enter keys together instead of just Enter key, and you can see the entered cell is still selected.

textFieldShouldReturn(_:), The Return key has the same function as the Enter key on a Windows keyboard. Both keys are used to submit data and create new lines when typing text. Important: The name "return" comes from a "carriage return," an old typewriter term. The carriage was a mechanism typewriters used to advance the paper to the next line. Since there is no carriage

Comments
  • How would I set them, Im so new at this and quite lost at the moment to be honest
  • By "them" I'm going to assume that you mean the delegates. Easiest way is to do this through the IB. Go to your storyboard->Click on each UITextField->Drag delegate over to the yellow ViewController icon. If you meant the tags, then you can also do this through the IB. Click on each UITextField and set the tags starting at 0 and increment by 1. You can also set both the delegates and tags programmatically in the ViewControllers viewDidLoad by setting textField.delegate = self and textField.tag = n
  • Post errors please. I made an edit and changed UIResponder to UIResponder!
  • I added a link to a sample project. Feel free to take a look and let me know if you get it solved. FYI: I set the delegates and the tags through the IB.
  • @meteorSD With an array of textfields? I am not sure if either practice is better than the other. Tags seem to be the easiest way when relying mostly on the Interface Builder.
  • Agree, prefer the case handling!
  • I'm not very sure I understand the coordinates part could you explain please?
  • sure. You wanna jump from the one on top to the one on the very bottom. So you sort it by y coordinate in array so in array[0] is the one on top, array[1] one under it and array[last] is the one on the bottom. Does this suffice?
  • The sorting is necessary because the text fields are not guaranteed to be ordered in the outlet collection: medium.com/@abhimuralidharan/…
  • Best solution (Swifty-er than the accepted one using tags) - worked perfectly!