我想做的是将2个文本字段加在一起,但我想将一个非整数与一个整数加在一起。因此,我想沿着a + 2 = 5进行操作。我想将int a定义为等于3。因此a = 3,a + 2在文本字段中应等于5。
@IBOutlet var txt1 : UITextfield!
@IBOutlet var txt2 : UITextfield!
@IBOutlet var total : UITextfield!
@IBAction func addTogether() {
//if txt1 = a then txt1 = 3
}
答案 0 :(得分:0)
以下是一个演示程序,可以执行您描述的操作,而无需在文本字段中明确插入“ a”。有3个字段,其规则为a + b = sum
。将修改其中的哪个取决于您正在编辑的字段。因此,如果先设置a
,然后再设置b
,您将看到sum
被修改。如果您随后开始编辑sum
,则会看到a
已根据b
和sum
进行了修改。
所有操作仅在演示代码中完成,因此鼓励您创建一个新项目,并用此代码替换ViewController
并尝试一下。我希望这足以使您走上所需的道路,但请注意,创建这样的系统通常需要完成许多工作。
import UIKit
class ViewController: UIViewController {
private var firstField: UITextField!
private var secondField: UITextField!
private var sumField: UITextField!
private var currentlyUsedField: UITextField?
private var previouslyUsedField: UITextField?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(white: 0.92, alpha: 1.0)
self.firstField = {
let field = UITextField(frame: CGRect(x: 12.0, y: 60.0 + (44.0+12.0)*0.0, width: view.bounds.width-24.0, height: 44.0))
field.keyboardType = .decimalPad
field.backgroundColor = UIColor.white
field.addTarget(self, action: #selector(onFieldDidChange), for: .editingChanged)
view.addSubview(field)
return field
}()
self.secondField = {
let field = UITextField(frame: CGRect(x: 12.0, y: 60.0 + (44.0+12.0)*1.0, width: view.bounds.width-24.0, height: 44.0))
field.keyboardType = .decimalPad
field.backgroundColor = UIColor.white
field.addTarget(self, action: #selector(onFieldDidChange), for: .editingChanged)
view.addSubview(field)
return field
}()
self.sumField = {
let field = UITextField(frame: CGRect(x: 12.0, y: 60.0 + (44.0+12.0)*2.0, width: view.bounds.width-24.0, height: 44.0))
field.keyboardType = .decimalPad
field.backgroundColor = UIColor.white
field.addTarget(self, action: #selector(onFieldDidChange), for: .editingChanged)
view.addSubview(field)
return field
}()
}
private func convertStringToDouble(_ string: String?) -> Double? {
// Maybe use some formatters here
let number = NSDecimalNumber(string: string)
let doubleNumber = number.doubleValue
return doubleNumber.isNaN ? nil : doubleNumber
}
private func convertDoubleToString(_ value: Double) -> String {
// Maybe use some formatters here
return String(value)
}
@objc private func onFieldDidChange(_ sender: UITextField) {
// Check if we need to update a chain of used fields
if sender !== currentlyUsedField {
previouslyUsedField = currentlyUsedField
currentlyUsedField = sender
}
// At least 2 fields must have been modified
guard let currentlyUsedField = currentlyUsedField, let previouslyUsedField = previouslyUsedField else { return }
// We need to know if field we are modifying is a sum or not
let fieldToModify = [firstField, secondField, sumField].filter { $0 !== currentlyUsedField && $0 != previouslyUsedField }.first!
if fieldToModify === sumField {
// Show the sum
if let left = convertStringToDouble(previouslyUsedField.text), let right = convertStringToDouble(currentlyUsedField.text) {
fieldToModify?.text = convertDoubleToString(left+right)
} else {
fieldToModify?.text = "UNDEFINED"
}
} else {
let leftField: UITextField = fieldToModify == firstField ? secondField : firstField
if let sum = convertStringToDouble(sumField.text), let subtract = convertStringToDouble(leftField.text) {
fieldToModify?.text = convertDoubleToString(sum-subtract)
} else {
fieldToModify?.text = "UNDEFINED"
}
}
}
}