Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';无法从swift上的两个UITextField获取结果_Swift_Uitextfield - Fatal编程技术网

Can';无法从swift上的两个UITextField获取结果

Can';无法从swift上的两个UITextField获取结果,swift,uitextfield,Swift,Uitextfield,两个UITextFields用于保存一个Int,然后resultText显示在名为resultText的UILabel中。我想您正在尝试从两个文本字段中添加数字。 为此,, 跟随变化 var widthValue : Int = 0 var heightValue : Int = 0 var result : Int = 0 @IBOutlet weak var widthText: UITextField! @IBOutlet weak var heightText: UITextField!

两个UITextFields用于保存一个
Int
,然后
resultText
显示在名为
resultText

UILabel
中。我想您正在尝试从两个文本字段中添加数字。 为此,, 跟随变化

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)"
}


您应该获得widthText和heightText的值。像这样的东西会有用的

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

“不起作用”是一个无用的问题描述。如果它没有编译,那么确切的错误消息是什么?如果它运行:实际结果和预期结果是什么?而“是我吗”的答案当然是“是”!感谢我在Xcode崩溃方面经历了太多问题,并给了我一些奇怪的错误,我完全弄糊涂了-我来自Xcode和C的早期,我不得不从头开始-你上面的代码可以工作,但只适用于整数和崩溃,比如说12.4。我不认为你有时间进行修复,但如果是这样,我将不胜感激它可以用一个固定的Double@JerGriffin很高兴它起作用了。如果有效,则将其标记为已接受的答案
let additionResult = widthText.text + heightText.text
resultText.text = additionResult
@IBAction func calculate(_ sender: UIView) {
  resultText.text  = String(Int(heightText.text!)! + Int(widthText.text!)!)
}