无法从swift上的两个UITextField获得结果

时间:2018-03-10 18:58:30

标签: swift uitextfield

var widthValue : Int = 0
var heightValue : Int = 0
var result : Int = 0
@IBOutlet weak var widthText: UITextField!
@IBOutlet weak var heightText: UITextField!
@IBOutlet weak var resultText: UILabel!

@IBAction func calculate(_ sender: Any) {
    widthText.text = "\(widthValue)"
    heightText.text = "\(heightValue)"
    resultText.text  = "\(widthValue + heightValue)"
}

两个UITextField用于在名为Int的{​​{1}}中显示resultText然后UILabel

2 个答案:

答案 0 :(得分:1)

我想你正在尝试从两个文本字段添加数字。 为了那个原因, 改变

resultText.text  = "\(widthValue + heightValue)"

let additionResult = widthText.text + heightText.text
resultText.text = additionResult

答案 1 :(得分:0)

您应该获取widthText和heightText的值。像这样的东西会起作用

@IBAction func calculate(_ sender: UIView) {
  resultText.text  = String(Int(heightText.text!)! + Int(widthText.text!)!)
}