码迷,mamicode.com
首页 > 编程语言 > 详细

Swift计算器

时间:2016-06-23 15:50:24      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

//

//  ViewController.swift

//  FristSwift

//

//  Created by Evan on 16/6/23.

//  Copyright © 2016年 Evan. All rights reserved.

//

 

import UIKit

 

let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width;

let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height;

 

class ViewController: UIViewController

{

    let textFile1:UITextField = UITextField(frame:CGRectZero)

    let plusLable:UILabel = UILabel(frame:CGRectZero)

    let textFile2:UITextField = UITextField(frame:CGRectZero)

    let equalLable:UILabel = UILabel(frame: CGRectZero)

    let resultLable:UILabel = UILabel(frame:CGRectZero)

    

    let button:UIButton = UIButton(frame:CGRectZero)

   

   override func viewDidLoad()

    {

        super.viewDidLoad()

        

        self.addContent();

        

        self.contentContraints();

    }

    

    func addContent()

    {

        textFile1.backgroundColor = UIColor.redColor()

        textFile1.keyboardType = UIKeyboardType.NumberPad

        self.view.addSubview(textFile1)

        

        plusLable.text = "+"

        plusLable.textColor = UIColor.greenColor()

        self.view.addSubview(plusLable)

        

        textFile2.backgroundColor = UIColor.redColor()

        textFile2.keyboardType = UIKeyboardType.NumberPad

        self.view.addSubview(textFile2)

        

        equalLable.text = "="

        equalLable.textColor = UIColor.greenColor()

        self.view.addSubview(equalLable)

        

        resultLable.backgroundColor = UIColor.redColor()

        self.view.addSubview(resultLable)

        

        button.backgroundColor = UIColor.redColor()

        button.setTitle("计算", forState:UIControlState.Normal)

        button.addTarget(self,action:Selector("buttonClick"),forControlEvents:UIControlEvents.TouchUpInside)

        self.view.addSubview(button)

    }

    

    func buttonClick()

    {

        let resultNum = (textFile1.text! as NSString).intValue + (textFile2.text! as NSString).intValue

        let resultStr = String(resultNum)

        resultLable.text = resultStr

        print(resultStr)

    }

      func contentContraints()

    {

        textFile1.frame = CGRectMake(10, 30, 50, 50)

        plusLable.frame = CGRectMake(65, 40, 20, 20)

        textFile2.frame = CGRectMake(80, 30, 50, 50)

        equalLable.frame = CGRectMake(135, 40, 20, 20)

        resultLable.frame = CGRectMake(155, 30, 50, 50)

        button.frame = CGRectMake(50, 100, 100, 30)

    }

}

 

Swift计算器

标签:

原文地址:http://www.cnblogs.com/happyEveryData/p/5610898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!