在 Return 键上启动 UIButton 的 IBAction

Initiate IBAction of a UIButton on Return key

我有一个注册表单,其中包含 UITextfieldsUIButton。我已经设置了 UITextfield 委托,以便当用户开始编辑 UITextfield 并按下 return 键时,它会转到下一个 UITextfield

DoB、ISD 代码、性别和国籍 UIButtons 其中有 IBActions,其余 UITextfields

是否可以在文本字段中按下 return 键时启动 IBActionUIButton。以便用户可以按顺序将必要的数据添加到注册表单中。

到目前为止我做了什么..

    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)

}

假设您的 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:) 在 [=25= 中实现] 以及在按钮完成其预期逻辑流程之后。

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)