Ios 成为第一响应者和多个UITextField焦点
因此,我有一个视图->UITextField&UILabel是两个子视图,我有多个包含子视图的视图。 我正在使用下面的委托函数,并将每个UITextfields指定为委托“textFieldShouldReturn(textField:UITextField)->Bool”。但是,当我按return键时,它们似乎不会改变textField的焦点。当对UITextFields使用此聚焦技术时,不会将其嵌套在视图中。它让我可以毫无疑问地改变焦点。为什么在视图中嵌套UITextField会导致无法让下一个UITextField成为第一响应者?我读过一些关于第一响应者及其工作原理的文章,但没有清楚地解释如何解决这个问题Ios 成为第一响应者和多个UITextField焦点,ios,swift,uitextfield,becomefirstresponder,Ios,Swift,Uitextfield,Becomefirstresponder,因此,我有一个视图->UITextField&UILabel是两个子视图,我有多个包含子视图的视图。 我正在使用下面的委托函数,并将每个UITextfields指定为委托“textFieldShouldReturn(textField:UITextField)->Bool”。但是,当我按return键时,它们似乎不会改变textField的焦点。当对UITextFields使用此聚焦技术时,不会将其嵌套在视图中。它让我可以毫无疑问地改变焦点。为什么在视图中嵌套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
}
class ScrollingViewWithFields:UIViewController、UIExtFieldDelegate{
让scrollView=UIScrollView()
让contentView=UIView()
var textFields:[UITextField]=[]
变量labeledTextField:[labeledTextField]=[]
重写func viewDidLoad(){
contentView.backgroundColor=UIColor.white
contentView.translatesAutoResizezingMaskintoConstraints=false
view.addSubview(滚动视图)
scrollView.addSubview(contentView)
scrollView.TranslatesAutoResizezingMaskintoConstraints=false
scrollView.backgroundColor=UIColor.white
让顶部=view.safeAreaLayoutGuide.topAnchor
let bottom=view.safeAreaLayoutGuide.bottomAnchor
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(相等:顶部),
scrollView.bottomAnchor.constraint(相等:底部),
scrollView.leftAnchor.constraint(equalTo:view.leftAnchor),
scrollView.rightAnchor.constraint(equalTo:view.rightAnchor)
])
设ltf=LabeledTextField()
ltf.translatesAutoResizezingMaskintoConstraints=false
contentView.addSubview(ltf)
填充(标题:“Hello”,字体:UIFont.systemFont(大小:14.0))
ltf.textField.delegate=self
ltf.textField.tag=0
设ltf2=LabeledTextField()
ltf2.TranslatesAutoResizezingMaskintoConstraints=假
contentView.addSubview(ltf2)
填充(标题:“What”,字体:UIFont.systemFont(of大小: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(等式:contentView.topAnchor,常量:8.0),
ltf.leadingAnchor.constraint(等式:contentView.leadingAnchor,常量:8.0),
ltf.trailingAnchor.constraint(等式:contentView.trailingAnchor,常量:8.0),
ltf.heightAnchor.constraint(相等常量:60)
])
NSLayoutConstraint.activate([
ltf2.topAnchor.constraint(等式:ltf.bottomAnchor,常数:8.0),
ltf2.leadingAnchor.constraint(等式:contentView.leadingAnchor,常数:8.0),
ltf2.trailingAnchor.constraint(等式:contentView.trailingAnchor,常量:8.0),
ltf2.heightAnchor.constraint(相等常量:60)
])
NSLayoutConstraint.activate([
contentView.topAnchor.constraint(equalTo:scrollView.topAnchor),
contentView.widthAnchor.constraint(等式:scrollView.widthAnchor),
contentView.bottomAnchor.constraint(等式:scrollView.bottomAnchor),
contentView.heightAnchor.constraint(equalToConstant:4000)
])
}
func textField应该返回(textField:UITextField)->Bool{
让tag=textField.tag
让下一步=标记+1
如果下一步
}问题在于如何在文本字段上设置标记并将其放入数组中
ltf.textField.tag = 0
ltf2.textField.tag = 1
self.textFields.append(ltf2.textField)
self.textFields.append(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
}
问题在于标记与数组中的顺序不匹配,因为数组最终将是[ltf2.textField,ltf.textField]
。我会完全跳过使用标记,只使用数组中的顺序
ltf.textField.tag = 0
ltf2.textField.tag = 1
self.textFields.append(ltf2.textField)
self.textFields.append(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
}
func textField应该返回(\utextfield:UITextField)->Bool{
如果let index=textFields.index(of:textField){
设nextIndex=index+1
让lastIndex=textFields.count-1
如果nextIndex您可以保留内存,而不是声明一个数组来存储textfield,然后尝试类似的方法
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
if let next = self.view.viewWithTag(textField.tag + 1) as? UITextField
{
next.becomeFirstResponder()
}
else
{
textField.resignFirstResponder()
}
return true
}