how to handle backspace in uitextfield

纵然是瞬间 提交于 2019-12-11 07:32:10

问题


I have 6 uitextfield.And i did the autofocus. once user enter one value in one textfield.They will automatically redirect to next textfield. But i dont know how to handle the backspace .Like when user in last textfield and press backspace to delete the value..again they have to move to 5,4,3,2,1 respective textfields.How to do that ??

Here my code for autofocus :

@IBAction func editing(_ sender: UITextField)
    {


        if sender == TextFieldOne {
            if sender.text?.characters.count == 1 {
                TextFieldTwo.becomeFirstResponder()
            }
        }

        else if sender == TextFieldTwo {
            if sender.text?.characters.count == 1 {
                TextFieldThree.becomeFirstResponder()
            }
        }

        else if sender == TextFieldThree {
            if sender.text?.characters.count == 1 {
                TextFieldFour.becomeFirstResponder()
            }
        }
        else if sender == TextFieldFour {
            if sender.text?.characters.count == 1 {
                TextFieldFive.becomeFirstResponder()
            }
        }
        else if sender == TextFieldFive {
            if sender.text?.characters.count == 1 {
                TextFieldSix.becomeFirstResponder()
            }
        }

        else if sender == TextFieldSix {
            if sender.text?.characters.count == 1 {
                TextFieldSix.resignFirstResponder()
            }
        }




    }

Thanks in advance !


回答1:


Swift 3.0

Remember editing(_:) action is bind using EditingChanged event.

if sender.text!.length == 0 {
       // textfield where you want to Focus.
       yourTextField.becomeFirstResponder()
 }

But I will suggest you to use below method of UITextFieldDelegate.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    }



回答2:


This should work:

// Array corresponding to the focus order of your textFields
let textFields = [TextFieldOne, TextFieldTwo, TextFieldThree, TextFieldFour, TextFieldFive, TextFieldSix]

func editing(_ sender: UITextField) {
    // Get sender index in textFields array
    guard let senderIndex = textFields.index(of: sender) else { return }

    // Char typed
    if sender.text?.characters.count == 1 {
        let nextTextFieldIndex = textFields.index(after: senderIndex)

        // Last textField: Remove focus
        if nextTextFieldIndex == textFields.count {
            textFields[senderIndex].resignFirstResponder()
        } else { // Other case: Focus next
            textFields[nextTextFieldIndex].becomeFirstResponder()
        }

    } else { // Char removed
        let previousTextFieldIndex = textFields.index(before: senderIndex)

        // If not first textField: Focus previous
        if previousTextFieldIndex != -1 {
            textFields[previousTextFieldIndex].becomeFirstResponder()
        }
    }
}



回答3:


First of all you shouldn't go to previous tf on tap on backspace as it's not standard in IOS. However here is the way you can achieve it:

In shouldchangecharacters method you need to identify if the tapped button is backspace, and then you use tags to identify each of the textfield seprately. So when you get the backspace you can get the previous textfield by decreasing the tag value from the current textfield and make the previous one the first responder.

It will be same for all the textfields so you don't have to use expensive if-else conditions to achieve it.



来源:https://stackoverflow.com/questions/45730245/how-to-handle-backspace-in-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!