Using “Next” as a Return Key

前端 未结 4 1149
深忆病人
深忆病人 2020-11-30 03:21

I use the \"Next\" value for the \"Return Key\" to get the Next button in place of the Done button, but (obviously) pressing it doesn\'t automatically move to the next UITex

4条回答
  •  旧时难觅i
    2020-11-30 03:50

    Just another way to achieve the result that I found helpful. My app had 11 text fields followed by a text view. I needed to be able to cycle through all fields using the next key and then resign the keyboard following the textview (i.e. other notes).

    In the storyboard, I set the tag on all of the fields (both text and textview) starting with 1 through 12, 12 being the textview.

    I'm sure there are other ways to do it and this method isn't perfect, but hopefully it helps someone.

    In code, I wrote the following:

    func textFieldShouldReturn(textField: UITextField) -> Bool {
    
        let nextTag = textField.tag + 1
    
        //Handle Textview transition, Textfield programmatically
        if textField.tag == 11 {
            //Current tag is 11, next field is a textview
            self.OtherNotes.becomeFirstResponder()
    
        } else if nextTag > 11 {
            //12 is the end, close keyboard
            textField.resignFirstResponder()
    
        } else {
            //Between 1 and 11 cycle through using next button
            let nextResponder = self.view.viewWithTag(nextTag) as? UITextField
            nextResponder?.becomeFirstResponder()
    
        }
    
        return false
    }
    
    func textFieldDidEndEditing(textField: UITextField) {
    
        textField.resignFirstResponder()
    }
    
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    
        //Remove keyboard when clicking Done on keyboard
        if(text == "\n") {
            textView.resignFirstResponder()
            return false
        }
        return true
    }
    

提交回复
热议问题