1

In OTP, there are four textfields were used. Moving the cursor to the previous textfield while pressing on keyboard back button?

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
mohan prasath
  • 11
  • 1
  • 2

3 Answers3

2

To detect the backspace event in a UITextField, first you need to set up a delegate for the UITextField and set it to self.

 class ViewController: UIViewController,UITextFieldDelegate

 self.textField.delegate = self

Then you use the delegate method below to detect if a backspace was pressed

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

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

    let char = string.cString(using: String.Encoding.utf8)
    let isBackSpace: Int = Int(strcmp(char, "\u{8}"))
    if isBackSpace == -8 {
        print("Backspace was pressed")
    }
            return true
}

Basically this method detects which button you are pressing (or have just pressed). This input comes in as an NSString. We convert this NSString to a C char type and then compare it to the traditional backspace character (\u{8}). Then if this strcmp is equal to -8, we can detect it as a backspace.

choice 2

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

    if (string.characters.count ) == 0 {
        //Delete any cases
        if range.length > 1 {
            //Delete whole word
        }
        else if range.length == 1 {
            //Delete single letter
        }
        else if range.length == 0 {
            //Tap delete key when textField empty
        }
    }
   return true
 }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • Won't simply checking replacement string to be equal to "" also work? – Rikh Mar 07 '17 at 07:30
  • @Rikh - it only check if empty, see in my comments `Basically this method detects which button you are pressing (or have just pressed)`, it – Anbu.Karthik Mar 07 '17 at 07:30
2

Make a subclass of UITextField. Then override the deleteBackward method. At last make custom delegate to detect backspace in your target class by confirming the BackSpaceDelegate protocol. Here give you a demo:

protocol BackSpaceDelegate {
    func deleteBackWord(textField: CustomTextField)
}

class CustomTextField: UITextField {
    var backSpaceDelegate: BackSpaceDelegate?
    override func deleteBackward() {
        super.deleteBackward()
        // called when textfield is empty. you can customize yourself.
        if text?.isEmpty ?? false {
             backSpaceDelegate?.deleteBackWord(textField: self)
        }
    }
}
class YourViewController: UIViewController, BackSpaceDelegate {

    func deleteBackWord(textField: CustomTextField) {
        /// do your stuff here. That means resign or become first responder your expected textfield.
    }
}

Hope this will help you.

Muzahid
  • 5,072
  • 2
  • 24
  • 42
0

Check the 'string' parameter in textFieldShouldChangeTextInRange (you got the name, didn't you?). If it is empty, the 'backspace' button was tapped.

If both 'string' param and the 'text' property of the text field are empty, then you can move to the previous text field.

Yevgeniy Leychenko
  • 1,287
  • 11
  • 25