我是 Swift 的新手,我正在将一个小表单实现到一个解决方案中。我有三个 UITextField,我想这样做,如果我输入一个词然后输入“空格键”,光标将移动到下一个 UITextField(即它会阻止您在该字段中输入多个词) .
如果我要这样做,我会怎么做?
我想我应该覆盖
textField(_:shouldChangeCharactersInRange:replacementString:)
方法 - 这是正确的吗?
谢谢!
最佳答案
试试这个
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
// Get your textFields text
let str = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)
// Check if the last character is a space
// If true then move to the next textfield
if str.characters.last! == " "{
print("SPACE!")
textField2.becomeFirstResponder()
}
else{
print(str.characters.last!)
}
return true
}
关于ios - 检测是否在 UITextField 中输入了 "space",并移动到下一个 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35852121/