阅读 213

Swift实现简单计算器项目

这篇文章主要为大家详细介绍了Swift实现简单计算器项目,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Swift实现简单计算器项目的具体代码,供大家参考,具体内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
//  ViewController.swift
//  计算器
//
//  Created by 悦兑科技 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()
 
    {
        // 计算
        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("计算", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.addTarget(self, action: "OnClick", forControlEvents: UIControlEvents.TouchUpInside)
 
        // 第一个数
        var numOne = UITextField(frame: CGRectMake(10, 200, 80, 30))
        numOne.placeholder = "输一个数"
        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)
   
        // 第二个数
        var numTwo = UITextField(frame: CGRectMake(120, 200, 80, 30))
        numTwo.placeholder = "输一个数"
        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  和
        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
 
    }
 
    /**
    计算按钮
    */
 
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()]
 
}
 
    /**
    点击界面事件
    */
 
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.numOne.resignFirstResponder()
        self.numTwo.resignFirstResponder()
 
    }
 
    /**
    清除按钮
    */
 
    func addCleanButton(){
 
        var cleanButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        var frame = CGRectMake(220, 240, 50, 30)
        cleanButton.frame = frame
        cleanButton.setTitle("清除", 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
 
    }
 
    /**
    清除按钮方法实现
    */
 
    func cleanButtonClick(){
 
    self.sum.text = "0"
    self.numOne.text = ""
    self.numTwo.text = ""
 
        if(self.sum.text=="0"){
        self.cleanButton.hidden = true     
 
        }
 
}
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐