如果序列只包含textfields或textview,我可以这样做。但混合序列怎么样?
答案 0 :(得分:1)
这不是很复杂。我通常使用一组视图维护序列:
// put your input fields here
let fields: [UIView] = [self.textField1, self.textView1, self.textField2]
func switchToNext(from field: UIView) {
guard let index = fields.index(of: field) else {
return
}
if index + 1 == fields.count {
// handle the last item
field.resignFirstResponder()
} else {
let next = fields[index + 1]
next.becomeFirstResponder()
}
}
您可以从UITextFieldDelegate
和UITextViewDelegate
调用该方法:
func textFieldShouldReturn(_ field: UITextField) {
switchToNext(from: field)
}