Ios 根据其他文本字段的输入,计算并返回文本字段中的值
所以,我要做的是将这些值输入textfield 1和textfield 2,textfield 3应该会自动计算并返回结果。在我的主板上,我已经添加了editingchanged并连接了@iAction。只有当我按下键盘上的“Backspace”(退格)按钮时,该功能才起作用。我的TableViewController功能如下所示 类SecondTableViewController:UITableViewController、UITextFieldDelegate{Ios 根据其他文本字段的输入,计算并返回文本字段中的值,ios,swift,textfield,Ios,Swift,Textfield,所以,我要做的是将这些值输入textfield 1和textfield 2,textfield 3应该会自动计算并返回结果。在我的主板上,我已经添加了editingchanged并连接了@iAction。只有当我按下键盘上的“Backspace”(退格)按钮时,该功能才起作用。我的TableViewController功能如下所示 类SecondTableViewController:UITableViewController、UITextFieldDelegate{ //identifier
//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仅在我标记实际文本字段并按下“Backspace按钮”时返回计算值
非常感谢您的帮助。谢谢。
CTRL+拖动您的textFields
到您的代码中,然后选择操作作为连接
将事件设置为“编辑已更改”
现在,在IBAction块中,做你想做的
试试这个:
Swift:
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)
}
}
实际上您想做什么?请查看func textField(textField:UITextField,shouldchangeCharactersinrange:NSRange,replacementString:string)->Bool
在UITextFieldDelegate:@KKRocks在textfield 1和textfield 2中输入值,textfield 3将自动输出乘法结果。我在第一句中写下了所有这些值?或者仅仅是应该自动输出值的一个值?所有主题都应该监控添加,但不幸的是,它们都不起作用。现在它不计算了全部。我不确定我是否理解你的问题。在我按照你的建议做了之后,我的所有文本字段中都有0.0,我无法覆盖它们。你为所有三个文本字段添加了不同的全部目标吗?是的。在主工具栏中,这三个字段都被委派了。我能够计算出计算部分,但我仍然无法将其写入文本字段300。因此estion是如何从ActionButton访问cell.ResultTextField?