我有一个SignUp表单,其中包含许多UITextfields
和UIButton
。我已经设置了UITextfield
个委托,以便当用户开始在UITextfield
上进行编辑并按下返回键时,它会转到下一个UITextfield
。
DoB,ISD代码,性别和国籍UIButtons
有IBActions
,其余为UITextfields
是否可以在文本字段中按IBAction
键时启动UIButton
return
。这样用户就可以按顺序将必要的数据添加到注册表单中。
到目前为止我做了什么..
func textFieldShouldReturn(_ textField: UITextField) -> Bool{
if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField
{
nextField.becomeFirstResponder()
}
else {
// Not found, so remove keyboard.
signupScrollView .setContentOffset(CGPoint( x: 0, y: 0), animated: true)
textField.resignFirstResponder()
return true
}
// Do not add a line break
return false
}
func textFieldDidBeginEditing(_ textField: UITextField) {
signupScrollView .setContentOffset(CGPoint( x: 0, y: textField.center.y-200), animated: true)
}
答案 0 :(得分:1)
假设您的textField
和button
有一个共同的标记序列,您基本上可以执行以下操作:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let nextView = textField.superview?.viewWithTag(textField.tag + 1)
if let nextTextField = nextView as? UITextField {
//next view is a textField so make next textField first responder
nextTextField.becomeFirstResponder()
}
//just this extra case is required
else if let nextButton = nextView as? UIButton {
//next view is a button so call it's associated action
//(assuming your button's @IBAction is on the Touch Up Inside event)
nextButton.sendActions(for: .touchUpInside)
/*
a button's action will be performed so if you want to
perform the return action on the textField then either
return true/false. Decide that as per your requirement
*/
return true
}
else {
//...
}
//...
}
上述逻辑足以回答您的问题,但只有在用户使用textField
且点按了键盘的Return
按钮时才有效。
现在......事情就是在按钮操作结束时,如果要继续下一个视图; textField
或button
,您可以 1 显式编码以使下一个视图处于活动状态。
示例:
ISD Code
完成后,您可以将移动textField
作为第一响应者Gender
完成后,您可以调用Nationality
我们可以使用公共帮助函数修改解决方案以处理textField
以及button
,我们将goNext(from:)
调用textFieldShouldReturn(_:)
作为func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let nextView = goNext(from: textField)
if let nextTextField = nextView as? UITextField {
//Next field was a textField so keyboard should stay
return false
}
textField.resignFirstResponder()
return true
}
@discardableResult func goNext(from sender: UIView) -> UIView? {
let nextView = sender.superview?.viewWithTag(sender.tag + 1)
print(nextView?.tag ?? "No view with tag \(sender.tag + 1)")
if let nextTextField = nextView as? UITextField {
nextTextField.becomeFirstResponder()
}
else if let nextButton = nextView as? UIButton {
nextButton.sendActions(for: .touchUpInside)
}
else {
print("Done")
signupScrollView.setContentOffset(CGPoint(x: 0, y: 0),
animated: true)
sender.resignFirstResponder()
}
return nextView
}
实现以及在按钮完成它的预期逻辑流程之后。
goNext(from: button)
现在对于按钮部分,只要相关按钮完成了它的逻辑,就需要执行goNext(from: dobButton)
。
示例:
用户已成功选择出生日期:您应该致电
Arrays.stream(array).forEachOrdered(System.out::println);