Ios 在返回键上启动UIButton的iAction

Ios 在返回键上启动UIButton的iAction,ios,swift,uitextfield,ibaction,uitextfielddelegate,Ios,Swift,Uitextfield,Ibaction,Uitextfielddelegate,我有一个注册表单,其中包括许多UITextfields和UIButton。我已经设置了UITextfield委托,这样当用户开始编辑UITextfield并按下返回键时,它会转到下一个UITextfield DoB、ISD代码、性别和国籍是UIButtons,具有iActions,其余为UITextfields 按文本字段中的return键,是否可以启动ui按钮的i操作。因此,用户可以顺序地向注册表单添加必要的数据 到目前为止我所做的 func textFieldShouldRetur

我有一个注册表单,其中包括许多
UITextfields
UIButton
。我已经设置了
UITextfield
委托,这样当用户开始编辑
UITextfield
并按下返回键时,它会转到下一个
UITextfield

DoB、ISD代码、性别和国籍是UIButtons,具有iActions,其余为UITextfields

按文本字段中的
return
键,是否可以启动
ui按钮的
i操作
。因此,用户可以顺序地向注册表单添加必要的数据

到目前为止我所做的

    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)

}

假设您的
文本字段
s和
按钮
s有一个共同的标记序列,您基本上可以执行以下操作:

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 {
        //...
    }
    
    //...
}

以上逻辑足以回答您的问题,但仅当用户位于
文本字段
上并点击键盘的
返回
按钮时,上述逻辑才起作用

现在。。。问题是,在一个按钮动作的末尾,如果你想继续下一个视图<代码>文本字段
按钮
,您可以明确编码以激活下一个视图

例如:

  • ISD code
    完成后,您可以将手机
    textField
    作为第一响应者
  • 完成
    性别
    后,您可以调用
    国籍
或者2。。。 我们可以修改解决方案来处理
textField
以及
button
s,类似于一个普通的辅助函数,我们将调用
goNext(from:)
textField shouldReturn(:)
中实现,以及在按钮完成其预期的逻辑流之后

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)


您不会触发
@IBAction
,您只需调用
@IBAction
调用的函数即可。您想通过编程方式调用
@IBAction
对吗?就像我在
Email
字段上,我从键盘上按
return
键,那么
DoB
操作应该会启动。@Kuldeep是的……如果我在else部分调用iAction,在iAction结束时调用initiate the textfield委托怎么办?@alpha47是你的
按钮和
textfield
s有共同点吗顺序标记?@alpha47当然,让我知道:)在
处获取错误,否则如果让nextButton=nextView as?ui按钮
“使用未解析的标识符'nextView'”检查
nextView
。在你做
之前,你应该有
let nextView=textField.superview?.viewWithTag(textField.tag+1)
如果let
成功了,我必须在DoB动作的末尾添加
isdButton.sendAction(for:.touchupine)
。为了启动
Mobile
textfield,在
isdButton
的动作结束时,让它成为第一个响应者。@alpha47真棒!但您不应在
DoB
按钮操作结束时立即调用
goNext(from:)
而应在用户选择出生日期后调用。