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
- 完成
后,您可以调用性别
国籍
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:)
而应在用户选择出生日期后调用。