zoukankan      html  css  js  c++  java
  • Swift计算器

    //

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

        }

    }

  • 相关阅读:
    设计模式-转载
    Java-类和对象基础练习
    Java-单例模式(singleton)-转载
    java-面向对象练习2
    Java-面向对象基础练习
    Java-字符串练习
    Java-数组练习5
    Java-数组练习4
    JAVA-初步认识-常用对象API(String类-常见功能-获取-1)
    JAVA-初步认识-常用对象API(String类-构造函数)
  • 原文地址:https://www.cnblogs.com/happyEveryData/p/5610898.html
Copyright © 2011-2022 走看看