gpt4 book ai didi

ios - BecomeFirstResponder 和多个 UITextField 焦点

转载 作者:行者123 更新时间:2023-11-28 14:55:48 25 4
gpt4 key购买 nike

所以我有一个 View -> UITextField 和 UILabel 是两个 child ,我有多个包含 child 的 View 。我正在使用下面的委托(delegate)函数,并将每个 UITextfields 分配为委托(delegate)“textFieldShouldReturn(_ textField: UITextField) -> Bool”。但是,当我按回车键时,它们似乎并没有改变 textField Focus。将此焦点技术与 UITextFields 一起使用,而无需将它们嵌套在 View 中。它使我可以毫无问题地改变焦点。为什么在 View 中嵌套 UITextField 会导致无法让下一个 UITextField 成为第一响应者?我已经阅读了一些关于第一响应者及其工作原理的内容,但它没有清楚地解释如何解决这个问题。

class ScrollingViewWithFields:UIViewController, UITextFieldDelegate {

let scrollView = UIScrollView()
let contentView = UIView()

var textFields:[UITextField] = []
var labeledTextField:[LabeledTextField] = []

override func viewDidLoad() {
contentView.backgroundColor = UIColor.white
contentView.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(scrollView)
scrollView.addSubview(contentView)

scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = UIColor.white
let top = view.safeAreaLayoutGuide.topAnchor
let bottom = view.safeAreaLayoutGuide.bottomAnchor

NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: top),
scrollView.bottomAnchor.constraint(equalTo: bottom),
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
scrollView.rightAnchor.constraint(equalTo: view.rightAnchor)
])



let ltf = LabeledTextField()
ltf.translatesAutoresizingMaskIntoConstraints = false

contentView.addSubview(ltf)
ltf.populate(title: "Hello", font: UIFont.systemFont(ofSize: 14.0))
ltf.textField.delegate = self
ltf.textField.tag = 0
let ltf2 = LabeledTextField()
ltf2.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(ltf2)
ltf2.populate(title: "What", font: UIFont.systemFont(ofSize: 14.0))
ltf2.textField.tag = 1
ltf2.textField.delegate = self



self.textFields.append(ltf2.textField)
self.textFields.append(ltf.textField)

NSLayoutConstraint.activate([
ltf.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8.0),
ltf.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0),
ltf.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 8.0),
ltf.heightAnchor.constraint(equalToConstant: 60)
])

NSLayoutConstraint.activate([
ltf2.topAnchor.constraint(equalTo: ltf.bottomAnchor, constant: 8.0),
ltf2.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8.0),
ltf2.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 8.0),
ltf2.heightAnchor.constraint(equalToConstant: 60)
])

NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.heightAnchor.constraint(equalToConstant:4000)
])

}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

let tag = textField.tag
let next = tag + 1

if next < self.textFields.count {
let textField = self.textFields[next]

textField.becomeFirstResponder()
self.scrollView.contentOffset = CGPoint(x: 0.0, y: textField.frame.origin.y - 8.0)

} else {
textField.resignFirstResponder()
}

return true
}

最佳答案

问题在于您在文本字段上设置标签并将它们放入数组的方式。

ltf.textField.tag = 0
ltf2.textField.tag = 1

self.textFields.append(ltf2.textField)
self.textFields.append(ltf.textField)

标签与数组中的顺序不匹配的问题,因为数组最终将成为 [ltf2.textField, ltf.textField]。我会完全跳过使用标签,而只使用数组中的顺序。

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if let index = textFields.index(of: textField) {
let nextIndex = index + 1
let lastIndex = textFields.count - 1

if nextIndex <= lastIndex {
textFields[nextIndex].becomeFirstResponder()
}
}
return true
}

关于ios - BecomeFirstResponder 和多个 UITextField 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49391238/

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