Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement the simple Calculator Project with Swift

Shulou Source: shulou.com Published: 2022-06-01 22:09:08 11月04日 Update

This article shows you how to implement the simple calculator project with Swift, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

/ ViewController.swift// Calculator / Created by Accord Technology on 15Universe / Copyright (c) 2015 BSY. All rights reserved.//import UIKitclass 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 () {/ / calculate var button: UIButton = UIButton.buttonWithType (UIButtonType.Custom) as UIButton var frame = CGRectMake 300,100,30) button.frame = frame self.view.addSubview (button) button.backgroundColor = UIColor.lightGrayColor () button.setTitle ("calculation", forState: UIControlState.Normal) button.setTitleColor (UIColor.blackColor (), forState: UIControlState.Normal) button.addTarget (self, action: "OnClick", forControlEvents: UIControlEvents.TouchUpInside) / / first number var numOne = UITextField (frame: CGRectMake (10,200,80) 30) numOne.placeholder = "enter a number" 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) / / the second number var numTwo = UITextField (frame: CGRectMake (120,200,80) 30) numTwo.placeholder = "enter a number" 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 and 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} / * * calculation button * / 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 ()]} / * * Click interface event * / override func touchesBegan (touches: NSSet) WithEvent event: UIEvent) {self.numOne.resignFirstResponder () self.numTwo.resignFirstResponder ()} / * * clear button * / func addCleanButton () {var cleanButton:UIButton = UIButton.buttonWithType (UIButtonType.Custom) as UIButton var frame = CGRectMake (220,240,50,30) cleanButton.frame = frame cleanButton.setTitle ("clear", 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} / * * clear button method implementation * / func cleanButtonClick () {self.sum.text= "0" self.numOne.text = "" self.numTwo.text = "if (self.sum.text==" 0 ") {self.cleanButton.hidden = true}

The above is how Swift implements the simple calculator project. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Number calculators buttons items content skills knowledge concise concise events that is articles methods more interfaces technology articles industries information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple Shulou Information Docker NVidia