简体   繁体   English

Swift 2:在UITextField中按Return键后调用方法

[英]Swift 2: Call a method after pressing Return in UITextField

I have a Text Field in a Tip Calculator. 我在提示计算器中有一个文本字段。 I want the Tip Calculator to update all the fields of my calculator. 我希望技巧计算器更新计算器的所有字段。

With the current code that I have now, for whatever reason. 无论出于何种原因,使用我现在拥有的当前代码。 It doesn't call calcTip() (or atleast it doesn't seem to) until I press return once, enter another (or the same) value and press return again. 直到我按回车一次,再输入另一个(或相同的)值,然后再次按回车,它才调用calcTip()(或至少它似乎没有)。

I'm really close to having this app work exactly how I want it to, and I feel like I'm just 1 or 2 lines away. 我真的很接近要让这个应用程序完全按照我想要的方式工作,我觉得我只有1或2行。

My ViewController conforms to UITextFieldDelegate, and I declared amountBeforeTaxTextField.delegate = self in viewDidLoad. 我的ViewController符合UITextFieldDelegate,我在viewDidLoad中声明了amountBeforeTaxTextField.delegate = self。

TextShouldReturn: TextShouldReturn:

func textFieldShouldReturn(textField: UITextField) -> Bool {
        amountBeforeTaxTextField.clearsOnBeginEditing = true
        amountBeforeTaxTextField.becomeFirstResponder()
        calcTip()
        amountBeforeTaxTextField.resignFirstResponder()
    return true
}

calcTip(): calcTip():

    func calcTip() {
    let tip = tipCalc.calculateTip()
    let total = tipCalc.calculateTotal()
    let tax = tipCalc.calculateTax()
    let perPerson = total / Float(Int(partySizeSlider.value))

    tipCalc.tipPercentage = (tipPercentageSlider.value)
    tipCalc.amountBeforeTax = (amountBeforeTaxTextField.text! as
        NSString).floatValue
    tipCalc.taxPercentage = (taxPercentageSlider.value)
    resultLabel.text = String(format: "Tip $%.2f, Tax: $%.2f Total: $%.2f", tip, tax, total)
    perPersonLabel.text = String(format: "Per-Person Total: $%.2f", perPerson)
}

Note: I have the same outcome with & without .becomeFirstResponder() 注意:无论是否使用.becomeFirstResponder(),我的结果相同

Thanks for reading. 谢谢阅读。

So your textFieldShouldReturn function is called when you press the bottom right button on the keyboard, which can have various titles; 因此,当您按下键盘上的右下按钮时,您的textFieldShouldReturn函数将被调用,该按钮可以具有各种标题。 such as "Go", "Return", "Done", or "Send". 例如“开始”,“返回”,“完成”或“发送”。

Your first line, only accomplishes a UI effect; 您的第一行只能完成UI效果; meaning when the user taps on the textfield to input text, there will be a little box that appears on the far right handside of the textfield that allows the user to clear the inputted text, if there is any. 意思是当用户点击文本框以输入文本时,在文本框的最右侧将出现一个小框,允许用户清除输入的文本(如果有)。 Sort of like a reset. 有点像重置。

When you use becomeFirstResponder, you are pretty much calling another keyboard to pop up, so you aren't accomplishing anything by doing that. 当您使用beginFirstResponder时,您几乎要调用另一个键盘来弹出,因此您无法完成任何操作。

When you use resignFirstResponder, you are hiding the keyboard. 使用resignFirstResponder时,您将隐藏键盘。

What you want to do is remove "becomeFirstResponder" as well as the first line, because those don't do anything to help your problem. 您要删除的是“ becomeFirstResponder”和第一行,因为这些都无济于事。 That should be the solution to the problem you are facing. 那应该是您面临的问题的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM