//  MHTAppDelegate.h

//  TestCa

//  Copyright (c) 2014年 Summer. All rights reserved.

#import
<UIKit/UIKit.h>

@interface MHTAppDelegate :
UIResponder <UIApplicationDelegate,UITextFieldDelegate>

@property (retain,
nonatomic) UIWindow *window;

@end

//  MHTAppDelegate.m

//  TestCal

//  Copyright (c) 2014年 Summer. All rights reserved.

#import
"MHTAppDelegate.h"

@interface
MHTAppDelegate()

{

UIView *_containerView;

UIButton *_desBut1;

UIButton *_desBut2;

UIButton *_desBut3;

UIButton *_desBut4;

UILabel *_hintLine;

UITextField *_textField;

BOOL  _isHave;

BOOL _isResult;

}

@property (retain,
nonatomic) NSString *_str;

@property (retain,
nonatomic) NSString *_number;

@end

@implementation MHTAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions

{

self.window = [[UIWindow
alloc] initWithFrame:[[UIScreen
mainScreen] bounds]];

// Override point for customization after application launch.

//创建底层的背景视图

_containerView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 320,
568)];

_containerView.backgroundColor = [UIColor
whiteColor];

[self.window
addSubview:_containerView];

[_containerView
release];

//baseCalculate

[self
baseCalculate];

//Line

[self
line];

_isHave = NO;

_isResult = NO;

self.window.backgroundColor = [UIColor
whiteColor];

[self.window
makeKeyAndVisible];

return YES;

}

- (void)baseCalculate

{int j =
0;

NSArray *calArr =@[@"C",
@"+/-", @"%", @"÷",
@"7", @"8", @"9",
@"x", @"4", @"5",
@"6", @"-", @"1",
@"2", @"3", @"+",
@"0", @".", @"="];

NSInteger n = 0;

for (int i = 0; i <
5; i++) {

for ( j = 0; j <
4; j++) {

if (i == 4 && j ==
0) {

_desBut1 = [UIButton
buttonWithType:UIButtonTypeSystem];

_desBut1.frame =
CGRectMake(0, 488,
160, 80);

[_desBut1
setTitle:calArr[n] forState:UIControlStateNormal];

[_desBut1
setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];

[_desBut1
addTarget:self
action:@selector(numberClick:)
forControlEvents:UIControlEventTouchUpInside];

_desBut1.titleLabel.font = [UIFont
fontWithName:@"AppleSDGothicNeo-Thin"
size:30];

_desBut1.backgroundColor = [UIColor
whiteColor];

[_containerView
addSubview:_desBut1];

j =
2;

n++;

}

_desBut1 = [UIButton
buttonWithType:UIButtonTypeSystem];

_desBut1.frame =
CGRectMake(0 + j * 80,
168 + 80 * i, 80,
80);

[_desBut1
setTitle:calArr[n] forState:UIControlStateNormal];

[_desBut1
setTitleColor:[UIColor
blackColor] forState:UIControlStateNormal];

if (j == 3) {

_desBut1.backgroundColor = [UIColor
orangeColor];

_desBut1.titleLabel.font = [UIFont
fontWithName:@"AppleSDGothicNeo-Thin"
size:30];

}else
if (i == 0){

_desBut1.backgroundColor = [UIColor
lightGrayColor];

}
else

{

_desBut1.backgroundColor = [UIColor
whiteColor];

}

_desBut1.titleLabel.font = [UIFont
fontWithName:@"AppleSDGothicNeo-Thin"
size:30];

[_containerView
addSubview:_desBut1];

if (n == 4 || n ==
5 || n == 6 || n == 8 || n ==
9 || n == 10 || n ==
12 || n == 13 || n == 14 || n ==
17 || n == 16) {

[_desBut1
addTarget:self
action:@selector(numberClick:)
forControlEvents:UIControlEventTouchUpInside];

}

if (n == 0) {

[_desBut1
addTarget:self
action:@selector(clearClick:)
forControlEvents:UIControlEventTouchUpInside];

}

if (n == 1) {

[_desBut1
addTarget:self
action:@selector(oppositeClick:)
forControlEvents:UIControlEventTouchUpInside];

}

if (n == 2) {

[_desBut1
addTarget:self
action:@selector(percentClick:)
forControlEvents:UIControlEventTouchUpInside];

}

if (n == 3 || n ==
7 || n == 11 || n == 15 || n ==
14) {

[_desBut1
addTarget:self
action:@selector(operationClick:)
forControlEvents:UIControlEventTouchUpInside];

}

if (n == 18) {

[_desBut1
addTarget:self
action:@selector(resultClick:)
forControlEvents:UIControlEventTouchUpInside];

}

n++;

}

j =
0;

}

NSLog(@"test:%d", n);

[self
displayTextField];

}

- (void)displayTextField

{

_textField = [[UITextField
alloc] initWithFrame:CGRectMake(0,
40, 320,
128)];

_textField.backgroundColor = [UIColor
blackColor];

_textField.text =
@"0";

//不可写

_textField.enabled =
NO;

_textField.delegate =
self;

_textField.textColor = [UIColor
whiteColor];

_textField.font = [UIFont
fontWithName:@"AppleSDGothicNeo-Thin"
size:40];

_textField.textAlignment =
NSTextAlignmentRight;

[_containerView
addSubview:_textField];

[_textField
release];

}

- (void)oppositeClick:(UIButton *)oppositeBut

{

CGFloat num = [_textField.text
floatValue];

_textField.text = [NSString
stringWithFormat:@"%g", (-num)];

}

- (void)percentClick:(UIButton *)percentBut

{

CGFloat num = [_textField.text
floatValue];

_textField.text = [NSString
stringWithFormat:@"%g", (num /
100)];

}

- (void)clearClick:(UIButton *)clearBut

{

_textField.text =
nil;

}

- (void)numberClick:(UIButton *)numBut

{

if ( _isHave || [_textField.text
isEqualToString:@"0"] ||
_isResult ) {

_textField.text =
@"";

_textField.text =  [_textField.text
stringByAppendingString:numBut.currentTitle];

}else{

_textField.text =  [_textField.text
stringByAppendingString:numBut.currentTitle];

}

_isHave = NO;

_isResult = NO;

}

- (void)operationClick:(UIButton *)operationBut

{

if ([operationBut.currentTitle
isEqualToString:@"+"]) {

self._str = [_textField.text
stringByAppendingString:@"+"];

_isHave = YES;

}
else if  ([operationBut.currentTitle
isEqualToString:@"-"]) {

self._str = [_textField.text
stringByAppendingString:@"-"];

_isHave = YES;

}
else if ([operationBut.currentTitle
isEqualToString:@"x"]) {

self._str = [_textField.text
stringByAppendingString:@"x"];

_isHave = YES;

}else
if ([operationBut.currentTitle
isEqualToString:@"÷"]) {

self._str = [_textField.text
stringByAppendingString:@"÷"];

_isHave = YES;

}

}

- (void)resultClick:(UIButton *)resultBut

{

NSLog(@"aa");

if ([self._str
hasSuffix:@"+"]) {

CGFloat value1 = [self._str
floatValue];

CGFloat value2 =  [_textField.text
floatValue];

_textField.text = [NSString
stringWithFormat:@"%g",(value1 + value2)];

self._str = @"";

_isResult = YES;

}else
if ([self._str
hasSuffix:@"-"]){

CGFloat value1 = [self._str
floatValue];

CGFloat value2 =  [_textField.text
floatValue];

_textField.text = [NSString
stringWithFormat:@"%g",(value1 - value2)];

self._str = @"";

_isResult = YES;

}else
if([self._str
hasSuffix:@"x"]){

CGFloat value1 = [self._str
floatValue];

CGFloat value2 =  [_textField.text
floatValue];

_textField.text = [NSString
stringWithFormat:@"%g",(value1 * value2)];

self._str = @"";

_isResult = YES;

}
else if([self._str
hasSuffix:@"÷"]){

CGFloat value1 = [self._str
floatValue];

CGFloat value2 =  [_textField.text
floatValue];

_textField.text = [NSString
stringWithFormat:@"%g",(value1 / value2)];

self._str = @"";

_isResult = YES;

}else{

self._str = @"";

_isResult = YES;

}

}

//收回键盘

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

return YES;

}

// Line

- (void)line

{

//Line

for (int i = 0; i <
4; i++) {

UILabel *hintLine = [[UILabel
alloc] initWithFrame:CGRectMake(0,
248 + 80 * i, 320,
0.5)];

hintLine.backgroundColor = [UIColor
blackColor];

[_containerView
addSubview:hintLine];

[hintLine
release];

}

//Line

for (int i = 0; i <
3; i++) {

if (i == 0) {

_hintLine = [[UILabel
alloc] initWithFrame:CGRectMake(80,
168, 0.5, 320)];

_hintLine.backgroundColor = [UIColor
blackColor];

[_containerView
addSubview:_hintLine];

[_hintLine
release];

}else{

_hintLine = [[UILabel
alloc] initWithFrame:CGRectMake(80 + i  *
80, 168, 0.5,
400)];

_hintLine.backgroundColor = [UIColor
blackColor];

[_containerView
addSubview:_hintLine];

[_hintLine
release];

}

}

}

- (void)applicationWillResignActive:(UIApplication *)application

{

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition
to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application

{

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application

{

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

- (void)dealloc

{

[_window
release];

[super
dealloc];

}

@end

UI 纯代码实现计算器的更多相关文章

  1. 猫学习IOS(三)UI纯代码UI——图片浏览器

    猫分享.必须精品 看看效果 主要实现相似看新闻的一个界面,不用拖拽,纯代码手工写. 首先分析app能够非常easy知道他这里有两个UILabel一个UIImageView还有两个UIButton 定义 ...

  2. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

  3. 纯代码写UI的时候,如何指定style?

    有的时候,需要使用纯代码实现Android UI,那么这个时候如何指定某个UI组件的样式呢? 一般来说,UI组件都有一些set方法可供使用,以调整一些UI属性,从而达到调整样式的目的. 但是,情况并非 ...

  4. 纯javascript代码编写计算器程序

    今天来分享一下用纯javascript代码编写的一个计算器程序,很多行业都能用到这个程序,例如做装修预算.贷款利率等等. 首先来看一下完成后的效果: 具体代码如下:(关注我的博客,及时获取最新WEB前 ...

  5. 拖图UI和纯代码UI

    1拖图UI, 优点:适合快速实验各种天马行空的想法 缺点:太多的storyBoard不好管理,不适合较大的项目,如果一个项目有价值,或成熟了,为了维护拓展,就最好改为纯代码 2纯代码UI 优点:1好维 ...

  6. AJ学IOS(17)UI之纯代码自定义Cell实现新浪微博UI

    AJ分享,必须精品 先看效果图 编程思路 代码创建Cell的步骤 1> 创建自定义Cell,继承自UITableViewCell 2> 根据需求,确定控件,并定义属性 3> 用get ...

  7. AJ学IOS(03)UI之纯代码实现UI——图片查看器

    AJ分享,必须精品 先看效果 主要实现类似看新闻的一个界面,不用拖拽,纯代码手工写. 首先分析app可以很容易知道他这里有两个UILabel一个UIImageView还有两个UIButton 定义UI ...

  8. Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局

    简介 简化iOS应用使用纯代码机型自适应布局的工作,使用一种简洁高效的语法替代NSLayoutConstraints. 项目主页: Masonry 最新示例: 点击下载 项目简议: 如果再看到关于纯代 ...

  9. 【iOS开发】多屏尺的自动适配 AutoLayout (纯代码方式)

    关于AutoLayout,最早从iOS6开始引入使用.   主要功能是使用约束,对视图进行相对布局,以适应不同屏尺的变换.   网上大量的资料都在介绍xib和storyboard,如何使用AutoLa ...

随机推荐

  1. Enable OWIN Cross-origin Request

    微软出了一套解决方式能够解决 "同意WebAPI的 CORS 请求" http://www.asp.net/web-api/overview/security/enabling-c ...

  2. cocos2D(四)---- CCSprite

    在介绍CCSprite之前,先要理解游戏开发中的一个核心概念:精灵.精灵也称为游戏对象,它能够用来表示游戏中的不论什么物体,比方敌人.子弹.甚至是一个背景图片.一段文字.CCSprite能够说是在co ...

  3. 每天进步一点点——Linux磁盘管理LVM与RAID

    转载请注明出处:http://blog.csdn.net/cywosp/article/details/38965799 1. 传统磁盘管理问题 当分区大小不够用时无法扩展其大小,仅仅能通过加入硬盘. ...

  4. 使用jquery+一般处理程序异步载入信息

    需求:有时候.web界面对性能要求比較高.我们就不考虑使用asp.net控件.而是使用html标签+jquery+一般处理程序来进行异步处理. watermark/2/text/aHR0cDovL2J ...

  5. 利用try-catch判断变量是已声明未声明还是未赋值

    原文 利用try-catch判断变量是已声明未声明还是未赋值 这篇文章主要介绍了利用try-catch判断变量是已声明未赋值还是未声明,需要的朋友可以参考下 目的是如果一个变量是已声明未赋值,就可以直 ...

  6. 命令行參数选项处理:getopt()及getopt_long()函数使用

         在执行某个程序的时候,我们通常使用命令行參数来进行配置其行为.命令行选项和參数控制 UNIX 程序,告知它们怎样动作. 当 gcc的程序启动代码调用我们的入口函数 main(int argc ...

  7. U8Linux磁盘与文件系统管理

    1.扇区为最小的物理存储单位,每个扇区为512bytes;将扇区组成一个圆,那就是柱面,柱面是分区的最小单位.Linux系统的EX2文件系统:inode的内容用于记录文件的权限与相关属性. 至于blo ...

  8. Oracle历史记录

    请问如何查询ORACLE的历史操作记录!!!!!------解决方案-------------------- 有一个专门存储操作的数据库表..select t.SQL_TEXT, t.FIRST_LO ...

  9. ecshop后台权限增加

    1.在后台“推荐管理”里添加“推荐人分成”.“会员分成”两个操作功能以及权限     index.php?act=menu     incluedes/inc_priv.php:权限对照表.inc_m ...

  10. 字符串拼接 拆分 NameValueCollection qscoll = HttpUtility.ParseQueryString(result)

    string result = "sms&stat=100&message=发送成功"; string d = HttpUtility.ParseQueryStri ...