本文实例为大家分享了swift实现简单计算器项目的具体代码,供大家参考,具体内容如下
// // viewcontroller.swift // 计算器 // // created by 悦兑科技 on 15/1/14. // d88尊龙官网手机app copyright (c) 2015年 bsy. all rights reserved. // import uikit class viewcontroller: uiviewcontroller { var numone = uitextfield() var numtwo = uitextfield() var cleanbutton = uibutton() var sum = uilabel() override func viewdidload() { super.viewdidload() self.view.backgroundcolor = uicolor.browncolor() [self .addallsubviews()] } func addallsubviews() { // 计算 var button:uibutton = uibutton.buttonwithtype(uibuttontype.custom) as uibutton var frame = cgrectmake(100, 300, 100, 30) button.frame = frame self.view.addsubview(button) button.backgroundcolor = uicolor.lightgraycolor() button.settitle("计算", forstate: uicontrolstate.normal) button.settitlecolor(uicolor.blackcolor(), forstate: uicontrolstate.normal) button.addtarget(self, action: "onclick", forcontrolevents: uicontrolevents.touchupinside) // 第一个数 var numone = uitextfield(frame: cgrectmake(10, 200, 80, 30)) numone.placeholder = "输一个数" numone.layer.bordercolor = uicolor.lightgraycolor().cgcolor numone.layer.borderwidth = 2 numone.layer.cornerradius = 5 self.view.addsubview(numone) numone.keyboardtype = uikeyboardtype.numberpad self.numone = numone var add = uilabel(frame: cgrectmake(100, 200, 20, 30)) add.text = " " add.font.fontwithsize(20) self.view.addsubview(add) // 第二个数 var numtwo = uitextfield(frame: cgrectmake(120, 200, 80, 30)) numtwo.placeholder = "输一个数" numtwo.layer.bordercolor = uicolor.lightgraycolor().cgcolor numtwo.layer.borderwidth = 2 numtwo.layer.cornerradius = 5 self.view.addsubview(numtwo) numtwo.keyboardtype = uikeyboardtype.numberpad self.numtwo = numtwo var equalto = uilabel(frame: cgrectmake(210, 200, 20, 30)) equalto.text = "=" equalto.font.fontwithsize(20) self.view.addsubview(equalto) // sum 和 var sum = uilabel(frame: cgrectmake(230, 200, 80, 30)) sum.textalignment = nstextalignment.center sum.font.fontwithsize(20) sum.text = "0" self.view.addsubview(sum) sum.layer.borderwidth = 2 sum.layer.bordercolor = uicolor.lightgraycolor().cgcolor self.sum = sum } /** 计算按钮 */ func onclick() { var sum = nsstring(string: self.numone.text).intvalue nsstring(string: self.numtwo.text).intvalue self.sum.text = nsstring(string: string(sum)) [self .addcleanbutton()] } /** 点击界面事件 */ override func touchesbegan(touches: nsset, withevent event: uievent) { self.numone.resignfirstresponder() self.numtwo.resignfirstresponder() } /** 清除按钮 */ func addcleanbutton(){ var cleanbutton:uibutton = uibutton.buttonwithtype(uibuttontype.custom) as uibutton var frame = cgrectmake(220, 240, 50, 30) cleanbutton.frame = frame cleanbutton.settitle("清除", forstate: uicontrolstate.normal) cleanbutton.settitlecolor(uicolor.redcolor(), forstate: uicontrolstate.normal) self.view.addsubview(cleanbutton) cleanbutton.addtarget(self, action: "cleanbuttonclick", forcontrolevents: uicontrolevents.touchupinside) self.cleanbutton = cleanbutton } /** 清除按钮方法实现 */ func cleanbuttonclick(){ self.sum.text = "0" self.numone.text = "" self.numtwo.text = "" if(self.sum.text=="0"){ self.cleanbutton.hidden = true } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。