mohan prasath
mohan prasath

Reputation: 21

want to know ever time while pressing on keyboard back button during textfield editing ios

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

Upvotes: 2

Views: 3210

Answers (3)

Muzahid
Muzahid

Reputation: 5186

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.

Upvotes: 2

Anbu.Karthik
Anbu.Karthik

Reputation: 82769

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
 }

Upvotes: 2

Ievgen Leichenko
Ievgen Leichenko

Reputation: 1317

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.

Upvotes: 0

Related Questions