根据其他文本字段的输入,计算并返回文本字段中的值

时间:2017-04-17 11:46:52

标签: ios swift textfield

所以,我要做的是将值输入到textfield 1和textfield 2中,textfield 3应该自动计算并返回结果。在我的主板上,我已经添加了editingchanged并连接了@IBAction。该功能仅在按下" Backspace"键盘上的按钮。我的TableViewController函数在

下面

类Sec​​ondTableViewController: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)")
}

因此,当我选择实际的TextField并按下" Backspace按钮"

时,ResultTextField仅返回计算值。

我将不胜感激任何帮助。谢谢。

2 个答案:

答案 0 :(得分:0)

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

将事件设置为Editing Changed

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

答案 1 :(得分:0)

试试这个:

<强>夫特:

There are 16 threads
Done making threads
Thread 4 (14) got 0
Thread 4 (14) got 1
Thread 8 (8) got 3
Thread 10 (6) got 4
Thread 6 (1) got 5
Thread 16 (5) got 2
Thread 3 (16) got 7
Thread 7 (8) got 8
Thread 7 (9) got 9
Thread 5 (3) got 6
Thread 3 (6) got 10
Thread 11 (2) got 11
Thread 14 (5) got 12
Thread 4 (16) got 13
Thread 16 (15) got 14 # <<
Thread 13 (11) got 15
Thread 4 (15) got 16 # <<
Thread 4 (15) got 17 # <<
Thread 4 (15) got 18 # <<
Thread 11 (15) got 19 # <<
Thread 13 (15) got 20 # <<
Thread 3 (15) got 21 # <<
Thread 9 (13) got 22
Thread 18 (15) got 23 # <<
Thread 18 (15) got 24 # <<
Thread 8 (13) got 25
Thread 7 (15) got 26 # <<
Thread 3 (15) got 27 # <<
Thread 7 (15) got 28 # <<
Thread 8 (15) got 29 # <<
Thread 13 (13) got 30
Thread 14 (13) got 31
Thread 8 (13) got 32
Thread 6 (13) got 33
Thread 9 (15) got 34 # <<
Thread 13 (15) got 35 # <<
Thread 9 (15) got 36 # <<
Thread 16 (15) got 37 # <<
Thread 3 (15) got 38 # <<
Thread 18 (13) got 39
Thread 3 (15) got 40 # <<
Thread 7 (14) got 41
Thread 12 (15) got 42 # <<
Thread 15 (15) got 43 # <<
Thread 4 (1) got 44
Thread 11 (1) got 45
Thread 7 (15) got 46 # <<
Thread 8 (15) got 47 # <<
Thread 7 (15) got 48 # <<
Thread 17 (15) got 49 # <<
Thread 10 (10) got 50
Thread 10 (15) got 51 # <<
Thread 11 (14) got 52
Thread 6 (8) got 53
Thread 5 (13) got 54
Thread 11 (15) got 55 # <<
Thread 11 (13) got 56
Thread 3 (13) got 57
Thread 7 (13) got 58
Thread 16 (16) got 59
Thread 5 (15) got 60 # <<
Thread 5 (15) got 61 # <<
Thread 6 (15) got 62 # <<
Thread 5 (15) got 63 # <<
Thread 5 (15) got 64 # <<
Thread 17 (11) got 65
Thread 15 (15) got 66 # <<
Thread 17 (15) got 67 # <<
Thread 11 (13) got 68
Thread 10 (15) got 69 # <<
Thread 3 (15) got 70 # <<
Thread 11 (15) got 71 # <<
Thread 6 (15) got 72 # <<
Thread 16 (13) got 73
Thread 6 (13) got 74
Thread 17 (15) got 75 # <<
Thread 4 (13) got 76
Thread 8 (13) got 77
Thread 12 (15) got 78 # <<
Thread 6 (11) got 79
Thread 3 (15) got 80 # <<
Thread 11 (13) got 81
Thread 7 (13) got 82
Thread 4 (15) got 83 # <<
Thread 7 (15) got 84 # <<
Thread 7 (15) got 85 # <<
Thread 10 (15) got 86 # <<
Thread 7 (15) got 87 # <<
Thread 12 (13) got 88
Thread 3 (13) got 89
Thread 18 (13) got 90
Thread 6 (13) got 91
Thread 18 (13) got 92
Thread 15 (15) got 93 # <<
Thread 16 (15) got 94 # <<
Thread 12 (15) got 95 # <<
Thread 17 (15) got 96 # <<
Thread 11 (13) got 97
Thread 15 (16) got 98
Thread 18 (7) got 99
Done sending
Done!

然后,实现回调函数:

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