gpt4 book ai didi

ios - 在 Xcode 8 和 Swift 3 中将 UITextField 转换为 Integer 并用它们计算

转载 作者:行者123 更新时间:2023-11-29 00:32:30 26 4
gpt4 key购买 nike

我搜索了整个互联网,但只找到了 Swift 2 或过时的 Xcode 版本的解决方案,这对我没有帮助:/另外,我对 Xcode 和 Swift 还很陌生,所以请原谅。

我希望用户在多个 UITextField 中输入一些数字,然后按“计算”按钮,在 UILabel 中显示结果。

到目前为止我测试了什么:

Convert input data to Integer in Swift

Swift calculate value of UITextFields and return value

所以到目前为止我的代码看起来真的很糟糕。没有任何效果,所以这就是我所拥有的一切,评论显示了我想要的样子:

import UIKit

class ViewController: UIViewController {

//elements from the viewcontroller
@IBOutlet weak var input1: UITextField!
@IBOutlet weak var input2: UITextField!
@IBOutlet weak var calculate: UIButton!
@IBOutlet weak var output: UILabel!

//calculation
//Just simple math like
//output = input1 + input2
//or output = (input1 + 50) + input2






override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

谢谢你帮我解决问题=)

最佳答案

首先,您需要通过从 Storyboard 中拖动来为您的按钮创建一个 IBAction。我认为这不是问题。

//imagine this is your IBAction function on calculate click
@IBAction func calculate(_ sender: UIView) {
output.text = String(Int(input1.text!)! + Int(input2.text!)!)
}

我跳过了所有验证,但对于快乐的路径它应该有效

关于ios - 在 Xcode 8 和 Swift 3 中将 UITextField 转换为 Integer 并用它们计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41395256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com