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

一笑奈何 提交于 2019-12-20 04:23:29

问题


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


回答1:


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
 }



回答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.




回答3:


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.



来源:https://stackoverflow.com/questions/42642357/want-to-know-ever-time-while-pressing-on-keyboard-back-button-during-textfield-e

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