【问题标题】:Calculate and return the value in the textfield, based on the input from the other textfield根据来自其他文本字段的输入计算并返回文本字段中的值
【发布时间】:2017-04-17 11:46:52
【问题描述】:

所以,我要做的是将值输入到文本字段 1 和文本字段 2 和文本字段 3 应该自动计算并返回结果。在我的主板中,我已经添加了editingchanged 并连接了@IBAction。该功能仅在我按下键盘上的“Backspace”按钮时才起作用。我的 TableViewController 功能如下

类 SecondTableViewController: UITableViewController, UITextFieldDelegate {

//identifier is saving 


@IBOutlet weak var Done: UIBarButtonItem!


var watts: Double = 0.0
var hours: Double = 0.0
var result: Double = 0.0
var hoursfromfunction: Double = 0.0
var wattsfromfunction: Double = 0.0
var receivedData = " "

override func viewDidLoad() {
    super.viewDidLoad()
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()

}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 2

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1

}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "saving", for: indexPath) as! SaveTableViewCell


    if (indexPath.section == 0) {
        cell.WattsOutput.placeholder = "Enter Watts. Can be found on label"
        cell.WattsOutput.keyboardType = UIKeyboardType.numbersAndPunctuation
        var textField100 = cell.WattsOutput
        textField100?.tag = 100
        textField100?.delegate = self
        //print ("Returned value is \(wattstext)")

        cell.HoursWeekOutput.placeholder = "Enter hours worked per week"
        cell.HoursWeekOutput.keyboardType = UIKeyboardType.numbersAndPunctuation
        var textField200 = cell.HoursWeekOutput
        textField200?.delegate = self
        textField200?.tag = 200

        var textField300 = cell.ResultTextField
        resultFieldChanged(textField300!)

        return cell
        }

    return cell
    }

func textFieldShouldReturn(_ textField: UITextField) -> Bool {

    if (textField.tag == 100){
        //print ("Should return is called and the value is \(textField.text!) W ")
        watts = Double(textField.text!)!
        calculatewatts(watts)
    }
    else if (textField.tag == 200) {
        //print ("Should return is called and the value is \(textField.text!) hours")
        hours = Double(textField.text!)!
        calculatehours(hours)
    }

    //resultFieldChanged(textField)

    return true
}


func calculatehours (_ arg: Double) -> Double {
    print ("The function calculateHours returns \(arg)")
    return arg
 }

func calculatewatts (_ arg: Double) -> Double {
    print("The function calculateWatts returns \(arg)")
    return arg
}


@IBAction func resultFieldChanged (_ textField: UITextField) {
    hoursfromfunction = calculatehours(hours)
    wattsfromfunction = calculatewatts(watts)
    result = hoursfromfunction * wattsfromfunction
    print ("Output of functions are \(hoursfromfunction) & \(wattsfromfunction)")

    textField.text? = "\(result)"
    print ("Result is \(result)")
}

所以,ResultTextField 只有在我选择实际的 TextField 并按下“退格键”时才会返回计算值

我将不胜感激。谢谢你。

【问题讨论】:

  • 你到底想做什么?
  • 在 UITextFieldDelegate 中查看func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Booldeveloper.apple.com/reference/uikit/uitextfielddelegate/…
  • @KKRocks 在文本域 1 和文本域 2 中输入值,文本域 3 将自动输出乘法结果。我第一句话就写好了

标签: ios swift textfield


【解决方案1】:

CTRL + Drag你的textFields到你的代码并选择Action作为连接

将事件设置为Editing Changed

现在,在 IBAction 块中做你想做的事!

【讨论】:

  • 全部?或者只是应该自动输出值的那个?
  • 所有主题都应该被监控
【解决方案2】:

试试这个:

斯威夫特:

yourTextfield.addTarget(self, action: #selector(textFieldDidChange(textField:)), for: .editingChanged)

然后,实现回调函数:

func textFieldDidChange(textField: UITextField){

 if (textField.tag == 100){
    //print ("Should return is called and the value is \(textField.text!) W ")
    watts = Double(textField.text!)!
    calculatewatts(watts)
}
else if (textField.tag == 200) {
    //print ("Should return is called and the value is \(textField.text!) hours")
    hours = Double(textField.text!)!
    calculatehours(hours)
}


}

【讨论】:

  • 不幸的是没有工作。现在它根本不计算。
  • 不确定我是否理解您的问题。在我按照你的建议做之后,我的所有文本字段中都保持 0.0,我无法覆盖它们
  • 您是否为所有三个文本字段添加了不同的所有目标?
  • 是的。在 mainstoryboard 中,这三个都是代表
  • 我能够弄清楚计算部分,但我仍然无法将其写入 textField300。那么,问题是如何从 ActionButton 访问 cell.ResultTextField?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 2019-10-01
  • 2019-07-20
相关资源
最近更新 更多