gpt4 book ai didi

Swift - 不为空时自动跳转到下一个文本字段

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:37 26 4
gpt4 key购买 nike

我正在开发一款游戏,用户必须在其中输入动词的过去式。我的 View 包含只接受一个字符的小文本框。截至目前,当前者包含字母时,我正在尝试自动跳转到下一个文本字段。

我想一直这样做,直到所有的盒子都被填满。用户还应该能够使用键盘上的返回按钮返回一个框。

下面是我目前正在使用的代码,但它没有跳转到下一个文本字段。我做错了什么?

 var game: Game? {
didSet {

if var answerContent = game?.answer {

let views = (0..<answerContent.characters.count).map { _ in UITextField(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) }

for textField in views {
textField.backgroundColor = UIColor.white
textField.textColor = Constants.MAIN_THEME_COLOR
textField.textAlignment = NSTextAlignment.center
textField.delegate = self
textField.returnKeyType = .next
textField.tag = views.index(of: textField)! + 1
self.container.addArrangedSubview(textField)

views.first?.becomeFirstResponder()
}
}
}
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return true }
let textLength = text.characters.count + string.characters.count - range.length

return textLength <= 1
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
print("Test")
nextField.becomeFirstResponder()
} else {
print("Test2")
textField.resignFirstResponder()
}

return false
}

更新代码 (24-04-2017) - 尝试跳转到下一个文本字段时返回 nil

var game: Game? {
didSet {

if var answerContent = game?.answer {

let views = (0..<answerContent.characters.count).map { _ in UITextField(frame: CGRect(x: 0, y: 0, width: 40, height: 40)) }

for textField in views {
textField.backgroundColor = UIColor.white
textField.textColor = Constants.MAIN_THEME_COLOR
textField.textAlignment = NSTextAlignment.center
textField.delegate = self
textField.addTarget(self, action: #selector(textChanged), for: .editingChanged)
textField.tag = views.index(of: textField)! + 1
self.container.addArrangedSubview(textField)
}

views.first?.becomeFirstResponder()
}
}
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
guard let text = textField.text else { return true }
let textLength = text.characters.count + string.characters.count - range.length

return textLength <= 1
}

func textChanged(sender: UITextField) {
if (sender.text?.characters.count)! > 0 {
print("Entered")
let nextField = textField?.superview?.viewWithTag(textField.tag + 1) as UIResponder!

if (nextField != nil) {
nextField?.becomeFirstResponder()
} else {
print("Error: nil found")
}
} else {
print("Removed")
textField?.resignFirstResponder()
}
}

回答:

var index: NSInteger = 0
for textField in views {
textField.backgroundColor = UIColor.white
textField.textColor = Constants.MAIN_THEME_COLOR
textField.textAlignment = NSTextAlignment.center
textField.autocapitalizationType = UITextAutocapitalizationType.none
textField.delegate = self
textField.addTarget(self, action: #selector(textChanged), for: .editingChanged)
textField.tag = index
self.container.addArrangedSubview(textField)

index+=1
}

func textChanged(sender: UITextField) {
if (sender.text?.characters.count)! > 0 {
let nextField = sender.superview?.viewWithTag(sender.tag + 1) as UIResponder!
nextField?.becomeFirstResponder()
} else {
sender.resignFirstResponder()
}
}

最佳答案

我建议你应该给控制事件添加一个目标.valueChanged:

// for each text field
textField.addTarget(self, action: #selector(textChanged), for: .valueChanged)

按如下方式实现textChanged:

func textChanged(sender: UITextField) {
if sender.text.characters.length > 0 {
let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField
nextField?.becomeFistResponder()
}
}

关于Swift - 不为空时自动跳转到下一个文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43573448/

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