BecomeFirstResponder和多个UITextField焦点

时间:2018-03-20 18:01:47

标签: ios swift uitextfield becomefirstresponder

所以我有一个观点 - > UITextField& UILabel是两个孩子,我有多个包含孩子的视图。 我正在使用下面的委托函数,并将每个UITextfields分配为委托" textFieldShouldReturn(_ textField:UITextField) - >布尔&#34 ;.但是,按下返回时,它们似乎不会更改textField Focus。将此焦点技术与UITextFields一起使用时,不要将它们嵌套在视图中。它允许我毫无问题地改变焦点。为什么在视图中嵌套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
}

}

2 个答案:

答案 0 :(得分:1)

问题在于您在文本字段上设置标记并将其放入数组中的方式。

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
}

答案 1 :(得分:0)

您可以保留内存而不是声明一个数组来填充textfeild并尝试这样的事情

def create