IOS–;在Swift中处理一次性代码
有没有办法通过限制其他键盘输入来处理一次性代码点击 我需要一个OTP文本字段来预先填充sms OTP代码。 用户不应使用键盘数字按钮手动输入OTP IOS 12我们有一次性代码选项,它将在QuickType栏中自动填充,用户可以点击它来填充文本字段。我找不到限制其他键盘输入的选项 有没有办法只允许一次代码点击并限制数字键盘的任何输入。 我尝试创建6个文本字段。 第一个文本字段内容类型设置为一次性代码。 UserInteractionEnabled仅用于第一个文本字段,因此用户不能在其他字段中输入数据。 一旦我得到OTP&taps QuickType条形码,我将在所有文本字段前添加前缀 viewdidload>IOS–;在Swift中处理一次性代码,ios,swift,keyboard,uitextfield,one-time-password,Ios,Swift,Keyboard,Uitextfield,One Time Password,有没有办法通过限制其他键盘输入来处理一次性代码点击 我需要一个OTP文本字段来预先填充sms OTP代码。 用户不应使用键盘数字按钮手动输入OTP IOS 12我们有一次性代码选项,它将在QuickType栏中自动填充,用户可以点击它来填充文本字段。我找不到限制其他键盘输入的选项 有没有办法只允许一次代码点击并限制数字键盘的任何输入。 我尝试创建6个文本字段。 第一个文本字段内容类型设置为一次性代码。 UserInteractionEnabled仅用于第一个文本字段,因此用户不能在其他字段中输入
otp1TB.addTarget(self, action:#selector(textFieldDidChange(_:)), for: .editingChanged)
@objc func textFieldDidChange(_ textField: UITextField) {
if let otpCode = textField.text , otpCode.count > 5 {
otp1TB.text = String(otpCode[otpCode.startIndex])
otp2TB.text = String(otpCode[otpCode.index(otpCode.startIndex, offsetBy: 1)])
otp3TB.text = String(otpCode[otpCode.index(otpCode.startIndex, offsetBy: 2)])
otp4TB.text = String(otpCode[otpCode.index(otpCode.startIndex, offsetBy: 3)])
otp5TB.text = String(otpCode[otpCode.index(otpCode.startIndex, offsetBy: 4)])
otp6TB.text = String(otpCode[otpCode.index(otpCode.startIndex, offsetBy: 5)])
otp1TB.isUserInteractionEnabled = false
self.view.endEditing(true)
}
}
这里的问题是,当键盘打开时,第一个文本字段处于活动状态,用户可以从键盘输入数字数据
当键盘打开时,我只需要允许一次代码点击。我认为您可以跳过整个复杂性,只需使用标准的iOS 12 OTP输入类型:
otpTextField.textContentType = .oneTimeCode
见:
2018年WWDC视频介绍了这一点:你可以试试我的第三方图书馆:- 解决方案:- 在viewDidLoad中,将标记和委托添加到所有
textfield
override func viewDidLoad() {
super.viewDidLoad()
otp1TB.delegate = self
otp1TB.tag = 1000
otp2TB.delegate = self
otp2TB.tag = 2000
otp3TB.delegate = self
otp3TB.tag = 3000
otp4TB.delegate = self
otp4TB.tag = 4000
otp5TB.delegate = self
otp5TB.tag = 5000
otp6TB.delegate = self
otp6TB.tag = 6000
}
在UITextFieldDelegate扩展中,实现shouldChangeCharacters在类似下面的函数中,它还将与textField.textContentType=.oneTimeCode
extension ViewController : UITextFieldDelegate {
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.trimmingCharacters(in: CharacterSet.whitespaces).count != 0 {
textField.text = string
if textField.tag < count*1000 {
let next = textField.superview?.viewWithTag((textField.tag/1000 + 1)*1000)
next?.becomeFirstResponder()
} else if textField.tag == count*1000 {
textField.resignFirstResponder()
}
} else if string.count == 0 { // is backspace
textField.text = ""
}
return false
}
}
扩展视图控制器:UITextFieldDelegate{
public func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
if string.trimmingCharacters(在:CharacterSet.whitespaces中).count!=0{
textField.text=字符串
如果textField.tag<计数*1000{
让next=textField.superview?.viewWithTag((textField.tag/1000+1)*1000)
下一个?.成为第一响应者()
}否则,如果textField.tag==count*1000{
textField.resignFirstResponder()辞职
}
}否则,如果string.count==0{//为退格
textField.text=“”
}
返回错误
}
}
您能分享一些到目前为止您尝试过的代码吗?在故事板中,第一个文本字段内容类型设置为一次性代码。我正在寻找一个解决方案,以限制其他键盘数字输入。对不起,这不是很清楚,在你的问题-这是很难遵循。听起来你想要这个:我试过了。如果语句“if textField.textContentType==UITextContentType.oneTimeCode”不太好,那么我建议重新编写您的问题,以明确问题所在,并解释它与您显示的代码的关系。如果不清楚,请抱歉。我已经更新了文本