【发布时间】: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) -> Bool:developer.apple.com/reference/uikit/uitextfielddelegate/… -
@KKRocks 在文本域 1 和文本域 2 中输入值,文本域 3 将自动输出乘法结果。我第一句话就写好了