【问题标题】:Initiate IBAction of a UIButton on Return key在返回键上启动 UIButton 的 IBAction
【发布时间】:2018-04-14 06:49:00
【问题描述】:

我有一个注册表单,其中包含多个 UITextfields 和 UIButton。我已经设置了UITextfield 代表,以便当用户开始在UITextfield 上编辑并按下返回键时,它会转到下一个UITextfield。

DoB、ISD Code、Gender 和 Nationality 为UIButtons,其中有IBActions,其余为UITextfields

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

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

    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)

}

【问题讨论】:

  • 你不会像这样触发@IBAction,你只需调用@IBaction调用的同一个函数。
  • 您想以编程方式调用@IBAction 对吗?就像我在Email 字段上一样,我从键盘上按下return 键,而不是DoB 动作应该触发。
  • @Kuldeep 是的...
  • 如果我在 else 部分调用 IBAction 并在 IBAction 结束时调用启动文本字段委托怎么办?
  • @alpha47 你的buttons 和textFields 有共同的顺序标签吗?

标签: ios swift uitextfield ibaction uitextfielddelegate


【解决方案1】:

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

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 的按钮操作

或者2...

我们可以修改解决方案来处理textField 和buttons 一样用一个通用的辅助函数,我们将在textFieldShouldReturn(_:) 中调用goNext(from:) 以及在按钮完成后实现这是预期的逻辑流程。

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)

【讨论】:

  • @alpha47 当然,让我知道 :)
  • 在else if let nextButton = nextView as? UIButton 出现错误“使用未解析的标识符'nextView'”
  • 检查nextView。在执行 if let 之前,您应该拥有 let nextView = textField.superview?.viewWithTag(textField.tag + 1)
  • 成功了,我必须在 DoB 的操作末尾添加isdButton.sendAction(for: .touchUpInside)。并启动Mobile 文本字段,使其成为isdButton 操作结束时的第一响应者..
  • @alpha47 太棒了!但您不应在DoB 按钮操作结束时立即调用goNext(from:),而应在用户选择出生日期后调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 2016-08-08
  • 1970-01-01
相关资源
最近更新 更多