Switching between Text fields on pressing return key in Swift

前端 未结 15 1450
名媛妹妹
名媛妹妹 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 10:02

    You can go with field tags. I think that's easier than other.

    First of all you have enter code hereto give tag to your field.

    On my code usernameField tag is 0 and passwordField tag is 1. And I check my tag. Then doing proccess.

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField.tag == 0 {
            passwordField.becomeFirstResponder()
        } else if textField.tag == 1 {
            self.view.endEditing(true)
            loginFunc()
        } else {
            print("Hata var")
    
        }
         return false
    }
    

    If click return on username field, go password. Or If you click return when password field, run login function to login.

提交回复
热议问题