在Return键上启动UIButton的IBAction

时间:2018-04-14 06:49:01

标签: ios swift uitextfield ibaction uitextfielddelegate

我有一个SignUp表单,其中包含许多UITextfieldsUIButton。我已经设置了UITextfield个委托,以便当用户开始在UITextfield上进行编辑并按下返回键时,它会转到下一个UITextfield

SignUp Form

DoB,ISD代码,性别和国籍UIButtonsIBActions,其余为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)

}

1 个答案:

答案 0 :(得分:1)

假设您的textFieldbutton有一个共同的标记序列,您基本上可以执行以下操作:

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按钮时才有效。

现在......事情就是在按钮操作结束时,如果要继续下一个视图; textFieldbutton,您可以 1 显式编码以使下一个视图处于活动状态。

示例:

  • ISD Code完成后,您可以将移动textField作为第一响应者
  • Gender完成后,您可以调用Nationality
  • 的按钮操作

2 ...

我们可以使用公共帮助函数修改解决方案以处理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);