UI —— 计算器

#import <UIKit/UIKit.h>

@interface MyViewController :UIViewController

{

NSInteger _firstName;

NSInteger _calculator;
// 数字的计数器

NSInteger _buttonType;
// 计算符号计数器

}

@property (nonatomic,retain)
UITextField *inputField;

@end


#import "MyViewController.h"

@interface
MyViewController ()

@end

@implementation MyViewController

-(void)dealloc

{

[_inputField
release];

[superdealloc];

}

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view.

//计算器的1 - 10个数字键

CGRect rect =
,,
, );

; i <
; i++) {

rect.;

NSString *s = [NSStringstringWithFormat:@"%d",
i];

UIButton *numButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

[numButtonsetFrame:rect];

[numButton setBackgroundColor:[UIColorcyanColor]];

[numButton setTitle:sforState:UIControlStateNormal];

[self.viewaddSubview:numButton];

[numButton addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];

}

CGRect textRect =
,
, , );

_inputField = [[UITextFieldalloc]
initWithFrame:textRect];

[_inputFieldsetKeyboardType:UIKeyboardTypeNamePhonePad];

[_inputFieldsetBackgroundColor:[UIColorgrayColor]];

[self.viewaddSubview:_inputField];

[_inputField
release];

textRect.;

textRect.;

UIButton *addButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

[addButtonsetFrame:textRect];

[addButton setBackgroundColor:[UIColorbrownColor]];

[addButton setTitle:@"="forState:UIControlStateNormal];

[self.viewaddSubview:addButton];

[addButton addTarget:selfaction:@selector(buttonEqual:)forControlEvents:UIControlEventTouchUpInside];

; j <
; j++) {

NSArray *symbolArray = [NSArrayarrayWithObjects:@"+",@"-",
@"*",@"/",
nil];

NSString *symbolString = [symbolArray
objectAtIndex:j];

textRect.;

UIButton *symbolButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

[symbolButtonsetFrame:textRect];

[symbolButtonsetBackgroundColor:[UIColorgreenColor]];

[symbolButtonsetTitle:symbolString
forState:UIControlStateNormal];

[self.viewaddSubview:symbolButton];

[symbolButtonsetTag:j];

[symbolButton addTarget:selfaction:@selector(calculateSymbolButton:)forControlEvents:UIControlEventTouchUpInside];

}

textRect.;

UIButton *clearButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

[clearButton setBackgroundColor:[UIColorpurpleColor]];

[clearButton setTitle:@"C"forState:UIControlStateNormal];

[clearButtonsetFrame:textRect];

[self.viewaddSubview:clearButton];

[clearButton addTarget:selfaction:@selector(clearButton:)forControlEvents:UIControlEventTouchUpInside];

}

- (void)buttonClick:(UIButton *)sender

{

NSString *inputString =
nil;

==
_calculator) {

inputString = [_inputField.textstringByAppendingString:sender.titleLabel.text];

}else{

inputString = sender.titleLabel.text;

}

_calculator =
;

[_inputFieldsetText:inputString];

}

- (void)calculateSymbolButton:(UIButton *)sender

{

//此处另一点小瑕疵,须要在推断一下

[selfbuttonEqual:nil];

_firstName = [_inputField.textintegerValue];

//保存输入的下一个数的形式

_calculator =
;

//
保存计算类型 + - / *(用tag更简单一点)

_buttonType = sender.tag;

}

- (void)clearButton:(UIButton *)sender

{

_firstName =
;

_calculator =
;

_buttonType =
;

_inputField.text =@"";

}

- (void)buttonEqual:(UIButton *)sender

{

NSInteger result;

switch (_buttonType) {

case
:

result =_firstName + [_inputField.textintegerValue];

break;

case
:

result =_firstName - [_inputField.textintegerValue];

break;

case
:

result =_firstName * [_inputField.textintegerValue];

break;

case
:

result =_firstName / [_inputField.textintegerValue];

break;

default:

break;

}

NSString *end = [NSStringstringWithFormat:@"%ld",
result];

_inputField.text = end;

}

版权声明:本文博主原创文章,博客,未经同意不得转载。

上一篇:斯坦福大学Andrew Ng教授主讲的《机器学习》公开课观后感[转]


下一篇:向MIP开源项目提交Issues