Switching between Text fields on pressing return key in Swift

前端 未结 15 1433
名媛妹妹
名媛妹妹 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:35

    the easiest way to change to next text Field is this no need for long code

    override func viewDidLoad() {
            super.viewDidLoad()
    
            emailTextField.delegate = self
            passwordTextField.delegate = self
        }
    
    
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            if textField == emailTextField {
                passwordTextField.becomeFirstResponder()
            }else {
                passwordTextField.resignFirstResponder()
            }
                return true
        }
    
    0 讨论(0)
  • 2020-12-04 09:35

    If you have a lot of textfield components my be it could be better to use an outlet collection, linking textfields and setting Return Key from interface builder

    @IBOutlet var formTextFields: [UITextField]!
    
    override func viewDidLoad() {
      for textField in formTextFields {
        textField.delegate = self
      }
    }
    
    extension RegisterViewController: UITextFieldDelegate {
      func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if let componentIndex = formTextFields.firstIndex(of: textField) {
          if textField.returnKeyType == .next,
            componentIndex < (formTextFields.count - 1) {
            formTextFields[componentIndex + 1].becomeFirstResponder()
          } else {
            textField.resignFirstResponder()
          }
        }
        return true
      }
    }
    
    0 讨论(0)
  • 2020-12-04 09:38

    Swift & Programmatically

    class MyViewController: UIViewController, UITextFieldDelegate {
    
        let textFieldA = UITextField()
        let textFieldB = UITextField()
        let textFieldC = UITextField()
        let textFieldD = UITextField()
    
        var textFields: [UITextField] {
            return [textFieldA, textFieldB, textFieldC, textFieldD]
        }
    
        override func viewDidLoad() {
            // layout textfields somewhere 
            // then set delegate
            textFields.forEach { $0.delegate = self }
        }
    
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            if let selectedTextFieldIndex = textFields.firstIndex(of: textField), selectedTextFieldIndex < textFields.count - 1 {
                textFields[selectedTextFieldIndex + 1].becomeFirstResponder()
            } else {
                textField.resignFirstResponder() // last textfield, dismiss keyboard directly
            }
            return true
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:42

    No any special, here is my currently using to change the textFiled. So the code in ViewController looks good :). #Swift4

    final class SomeTextFiled: UITextField {
    
      public var actionKeyboardReturn: (() -> ())?
    
      override init(frame: CGRect) {
          super.init(frame: frame)
          super.delegate = self
      }
    
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
          fatalError("init(coder:) has not been implemented")
      }
    
      func textFieldShouldReturn(_ textField: UITextField) -> Bool {
          self.resignFirstResponder()
          actionKeyboardReturn?()
          return true
       }
    }
    
    extension SomeTextFiled: UITextFieldDelegate {}
    
    
    class MyViewController : UIViewController {
    
        var tfName: SomeTextFiled!
        var tfEmail: SomeTextFiled!
        var tfPassword: SomeTextFiled!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tfName = SomeTextFiled(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
            tfName.actionKeyboardReturn = { [weak self] in
                self?.tfEmail.becomeFirstResponder()
            }
            tfEmail = SomeTextFiled(frame: CGRect(x: 100, y: 0, width: 100, height: 100))
            tfEmail.actionKeyboardReturn = { [weak self] in
                self?.tfPassword.becomeFirstResponder()
            }
            tfPassword = SomeTextFiled(frame: CGRect(x: 200, y: 0, width: 100, height: 100))
            tfPassword.actionKeyboardReturn = {
                /// Do some further code
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 09:46

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

    Caleb's version in Swift 4.0

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if let nextField = self.view.viewWithTag(textField.tag + 1) as? UITextField {
            nextField.becomeFirstResponder()
        } else {
            textField.resignFirstResponder()
        }
        return false
    }
    

    P.S. textField.superview? not working for me

    0 讨论(0)
提交回复
热议问题