heqiang0 2020-03-02
先看图,要先把Main.storyboard里面的大致画好
这里用到4个Label,2个Text Field和1个Button具体的看下图
然后这是ViewController.swift(也就是代码区),看图
附上代码(需要的自己复制粘贴很贴心吧):
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textNumber1: UITextField! @IBOutlet weak var textNumber2: UITextField! @IBOutlet weak var resultLabel: UILabel! @IBAction func calculate(_ sender: UIButton) { let num1 = Int(textNumber1.text!)! let num2 = Int(textNumber2.text!)! let sum = num1+num2 resultLabel.text = String(sum) }
}
最后附上模拟器运行效果图