gpt4 book ai didi

ios - 想知道在文本字段编辑 ios 期间按下键盘后退按钮时的任何时间

转载 作者:搜寻专家 更新时间:2023-11-01 06:16:56 26 4
gpt4 key购买 nike

在 OTP 中,使用了四个文本字段。按下键盘后退按钮时将光标移动到上一个文本字段?

最佳答案

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.

选择 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
}

关于ios - 想知道在文本字段编辑 ios 期间按下键盘后退按钮时的任何时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42642357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com