網站首頁 編程語言 正文
本文實例為大家分享了Swift實現簡單計算器項目的具體代碼,供大家參考,具體內容如下
// // ?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 ? ?? ? ? ? ? } } }
原文鏈接:https://blog.csdn.net/mjbaishiyun/article/details/42736675
相關推薦
- 2022-08-21 Android使用貝塞爾曲線畫心形_Android
- 2022-10-26 使用Go?http重試請求的示例_Golang
- 2022-09-08 Go語言里切片slice的用法介紹_Golang
- 2022-09-19 LyScript尋找ROP漏洞指令片段的方法詳解_python
- 2022-09-08 Pytorch中expand()的使用(擴展某個維度)_python
- 2022-09-30 GO中sync包自由控制并發示例詳解_Golang
- 2022-05-23 Nginx工作模式及代理配置的使用細節_nginx
- 2022-11-18 詳解C語言內核字符串拷貝與比較_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支