我正在处理4位数的密码,我已经对它进行了大量的搜索,但遗憾的是我无法找到swift 2.0的相关示例。我所追求的是在提供4位数密码后代表用户主页。
我的代码是从一个Objective-c示例中复制而来的,并没有像想要的那样完成所有功能。到目前为止,它正在设置响应者,当用户输入密码时,它使用setNextResponder函数设置下一个响应者。
正如您所看到的,shouldChangeCharactersInRange函数如下所示接受正向响应和退格响应。但是,我无法实现全部功能。当我说全部功能将光标移动到UITextfield下一个直到最后一个并且从最后一个到第一个的退格是好的但是例如如果第二个响应者我不能退格或当我从最后一个退格并且当响应者来到第三个UITextfield时我无法输入并再次移动光标第四个。
您可以看到我的ShouldCharacterChange方法如下:
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let scanner = NSScanner(string:string)
if (string.characters.count > 0 && !scanner.scanInt(nil) ){
return false
}
let oldLength = textField.text!.utf16.count
let replaceStringLength = string.characters.count
let rangeLength = range.length
let newLength = oldLength - rangeLength + replaceStringLength
var shouldBackspace = false
if (newLength == 1){
if (textField == firstFieldTextField){
self.performSelector(Selector("setNextResponder:") ,withObject: secondField, afterDelay: 0.1)
shouldBackspace = true
print (shouldBackspace)
} else if (textField == secondField) {
self.performSelector(Selector("setNextResponder:") ,withObject: thirdField, afterDelay: 0.1)
} else if (textField == thirdField){
self.performSelector(Selector("setNextResponder:") ,withObject: fourthField, afterDelay: 0.1)
}
} else if (oldLength > 0 && newLength == 0) {
if (textField == fourthField){
self.performSelector(Selector("setNextResponder:") ,withObject: thirdField, afterDelay: 0.1)
} else if (textField == thirdField){
self.performSelector(Selector("setNextResponder:") ,withObject: secondField, afterDelay: 0.1)
} else if (textField == secondField){
self.performSelector(Selector("setNextResponder:") ,withObject: firstFieldTextField, afterDelay: 0.1)
}
}
return newLength <= 1 // Bool
}
SetNextResponder函数或选择器的选择器如下所示:
func setNextResponder(textField:UITextField){
textField.becomeFirstResponder()
}
The video of the application can be found here for more detailed explanation
非常感谢任何帮助