Simple calculator project for Swift development, swift development Calculator

Source: Internet
Author: User

Simple calculator project for Swift development, swift development Calculator

//

// ViewController. swift

// Calculator

//

// Created by yueda Technology on 15/1/14.

// 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 ()

{

// Computing

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 ("calculation", forState: UIControlState. Normal)

Button. setTitleColor (UIColor. blackColor (), forState: UIControlState. Normal)

Button. addTarget (self, action: "OnClick", forControlEvents: UIControlEvents. TouchUpInside)

// The 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 failed to = UILabel (frame: CGRectMake (210,200, 20, 30 ))

Failed to. text = "="

Failed to. font. fontWithSize (20)

Self. view. addSubview (similar)

// 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


}

/**

Computing 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

}


}

}





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.