Switching between Text fields on pressing return key in Swift

前端 未结 15 1435
名媛妹妹
名媛妹妹 2020-12-04 09:25

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

相关标签:
15条回答
  • 2020-12-04 09:50

    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
    }
    
    0 讨论(0)
  • 2020-12-04 09:51

    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
    
        }
    
    0 讨论(0)
  • 2020-12-04 09:51

    Just use becomeFirstResponder() method of UIResponder class in your textFieldShouldReturn method. Every UIView objects are UIResponder's subclasses.

    if self.emaillabel.isEqual(self.anotherTextField)
    {
        self.anotherTextField.becomeFirstResponder()
    }
    

    You can find more information about becomeFirstResponder() method at Apple Doc's in here.

    0 讨论(0)
  • 2020-12-04 09:53

    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
    • Create an IBOutlet for all TextFields

      class ViewController: UIViewController, UITextFieldDelegate {
      
        @IBOutlet weak var txtFieldName: UITextField!
        @IBOutlet weak var txtFieldEmail: UITextField!
        @IBOutlet weak var txtFieldPassword: UITextField!
      
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
          if textField == txtFieldName {
             textField.resignFirstResponder()
             txtFieldEmail.becomeFirstResponder()
          } else if textField == txtFieldEmail {
             textField.resignFirstResponder()
             txtFieldPassword.becomeFirstResponder()
          } else if textField == txtFieldPassword {
             textField.resignFirstResponder()
          }
         return true
        }
      }
      
    0 讨论(0)
  • 2020-12-04 09:56

    I have a good solution for your question.

    STEP:

    1 - Set your return key from the storyboard.

    2 - In your swift file.

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            if textField.returnKeyType == .next {
                Email.resignFirstResponder()
                Password.becomeFirstResponder()
            } else if textField.returnKeyType == .go {
                Password.resignFirstResponder()
                self.Login_Action()
            }
            return true
        }
    

    3 - Don't forget to set the delegate of the Textfield.

    Thank you :)

    0 讨论(0)
  • 2020-12-04 09:58

    An alternative method for purists who don't like using tags, and wants the UITextField delegate to be the cell to keep the components separated or uni-directional...

    1. Create a new protocol to link the Cell's and the TableViewController.

      protocol CellResponder {
        func setNextResponder(_ fromCell: UITableViewCell)
      }
      
    2. Add the protocol to your cell, where your TextField Delegate is also the cell (I do this in the Storyboard).

      class MyTableViewCell: UITableViewCell, UITextFieldDelegate {
        var responder: CellResponder?
      
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
          responder?.setNextResponder(self)
          return true
        }
      }
      
    3. Make your TableViewController conform to the CellResponder protocol (i.e. class MyTableViewController: UITableViewController, CellResponder) and implement the method as you wish. I.e. if you have different cell types then you could do this, likewise you could pass in the IndexPath, use a tag, etc.. Don't forget to set cell.responder = self in cellForRow..

      func setNextResponder(_ fromCell: UITableViewCell) {
        if fromCell is MyTableViewCell, let nextCell = tableView.cellForRow(at: IndexPath(row: 1, section: 0)) as? MySecondTableViewCell {
      
          nextCell.aTextField?.becomeFirstResponder()
      
        } ....
      }
      
    0 讨论(0)
提交回复
热议问题