Switching between Text fields on pressing return key in Swift

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

    Swift 4.2

    This is a More Generic and Easiest Solution, you can use this code with any amount of TextFields. Just inherit UITextFieldDelegate and update the Textfield Tag according to the order and copy this function

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

提交回复
热议问题