ios - 检测是否在 UITextField 中输入了 "space",并移动到下一个 UITextField

标签 ios swift uitextfield

我是 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/

相关文章:

swift - UIView 中的自定义 View 的触摸检测问题,而不是 UIScrollView

ios - 如何最好地处理核心数据 + iOS 状态恢复?

ios - 在 tableviewheader 中使用 UITextField 进行 self.view.endEditing 时崩溃

ios - 在 UITableViewController textfield.becomeFirstResponder() 中不滚动 tableview

ios - ResetPassword 中警报中的可选文本 - 使用 Swift 的 iOS 项目

ios - Swift 无法更新 Parse 对象

swift - 无法调用 NSManagedObject 类上的指定初始化程序 -- CoreData

ios:自定义 UITableViewCell 为空

ios - 使 UIPickerView 宽度填充 View

ios - 如何在 UIActionSheet 中的文本旁边插入图像?