//// VIEwController.swift// Swift加法计算器//// Created by hehongbo on 16/8/31.// copyright © 2016年 hhb. All rights reserved.//import UIKitclass VIEwController: UIVIEwController { var numtext1:UITextFIEld? var numtext2:UITextFIEld? var resultLabel:UILabel? overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() setupUI() } // 计算 func clac() { print("\(numtext1?.text) == \(numtext2?.text)") // 将文本框内容转换为数值 // 先测试:let num1 = Int(numtext1?.text ?? "")// let num1 = Int(numtext1?.text ?? "")// let num2 = Int(numtext2?.text ?? "") guard let num1 = Int(numtext1?.text ?? ""),num2 = Int(numtext2?.text ?? "") else { print("必须都输入数字才能计算") return } // 处理结果 resultLabel?.text = "\(num1 + num2)" } func setupUI() -> () { // 1.两个textFIEld let tf1 = UITextFIEld(frame: CGRect(x: 20,y: 20,wIDth: 100,height: 30)) tf1.borderStyle = .roundedRect tf1.text = "0" vIEw.addSubvIEw(tf1) let tf2 = UITextFIEld(frame: CGRect(x: 140,height: 30)) tf2.borderStyle = .roundedRect tf2.text = "0" vIEw.addSubvIEw(tf2) // 记录属性 numtext1 = tf1 numtext2 = tf2 // 2.三个label let l1 = UILabel(frame: CGRect(x: 120,wIDth: 20,height: 30)) l1.text = "+" l1.textAlignment = .center vIEw.addSubvIEw(l1) let l2 = UILabel(frame: CGRect(x: 240,height: 30)) l2.text = "=" l2.textAlignment = .center vIEw.addSubvIEw(l2) let l3 = UILabel(frame: CGRect(x: 250,wIDth: 60,height: 30)) l3.text = "0" l3.textAlignment = .right vIEw.addSubvIEw(l3) resultLabel = l3 // 3.一个button let btn = UIbutton() btn.setTitle("计算",for: UIControlState(rawValue: 0)) btn.setTitlecolor(UIcolor.black(),for: UIControlState(rawValue: 0)) btn.sizetoFit() btn.center = vIEw.center vIEw.addSubvIEw(btn) btn.addTarget(self,action: #selector(clac),for: .touchUpInsIDe) }}总结
以上是内存溢出为你收集整理的Swift加法计算器全部内容,希望文章能够帮你解决Swift加法计算器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)