In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.