How to listen to UIReturnKeyType.Next with Swift iOS

When the user presses the next on the keyboard, I want to move from the current UITextField to the next UITextField, UIReturnKeyType is set to UIReturnKeyType.Next

This is how I configured UITextField.

username.returnKeyType = UIReturnKeyType.Next
username.textColor = UIColor.whiteColor()
username.placeholder = "Username"
username.borderStyle = UITextBorderStyle.RoundedRect
uUsername.textAlignment = NSTextAlignment.Center
username.backgroundColor = UIColor.lightGrayColor()
username.font = UIFont(name: "Avenir Next", size: 14)
username.autocapitalizationType = UITextAutocapitalizationType.None
username.autocorrectionType = UITextAutocorrectionType.No
+4
source share
4 answers

You need to configure the UITextFieldDelegate method textFieldShouldReturn:. From this method, you can check which text field is returned and reassign the state of the first responder accordingly. Of course, this means that you have to designate your class as a delegate of text fields.

Example:

class MyClass: UITextFieldDelegate {

    func setup() { // meaningless method name
        textField1.delegate = self
        textField2.delegate = self
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        if (textField === textField1) {
            textField2.becomeFirstResponder()
        } else if (textField === textField2) {
            textField2.resignFirstResponder()
        } else {
            // etc
        }

        return true
    }
}
+20
source

Follow these steps:

1) xib/Storyboard .

2) .

3) , :

func textFieldShouldReturn(textField: UITextField) -> Bool
{
    let nextTag: Int = textField.tag + 1

    let nextResponder: UIResponder? = textField.superview?.superview?.viewWithTag(nextTag)

    if let nextR = nextResponder
    {
        // Found next responder, so set it.
        nextR.becomeFirstResponder()
    }
    else
    {
        // Not found, so remove keyboard.
        textField.resignFirstResponder()
    }

    return false
}

, !

+5

If you have two text fields: - textField1 - textField2

func textFieldShouldReturn(textField: UITextField!) -> Bool {

    textField1.resignFirstResponder()
    textField2.becomeFirstResponder()

    return true

}
+2
source
class ViewController: UIViewController,UITextFieldDelegate    //set delegate to class

@IBOutlet var txtValue: UITextField    //create a textfield variable

override func viewDidLoad() { 
super.viewDidLoad() 
txtValue.delegate = self //set delegate to textfile 
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {   //delegate method
return true
}
0
source

Source: https://habr.com/ru/post/1544351/


All Articles