#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 = CGRectMake(40,0, 50, 30);
for (int i =0; i < 10; i++) {
rect.origin.y +=50;
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 = CGRectMake(130, 50, 80, 30);
_inputField = [[UITextFieldalloc] initWithFrame:textRect];
[_inputFieldsetKeyboardType:UIKeyboardTypeNamePhonePad];
[_inputFieldsetBackgroundColor:[UIColorgrayColor]];
[self.viewaddSubview:_inputField];
[_inputField release];
textRect.size.width =50;
textRect.origin.y +=50;
UIButton *addButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[addButtonsetFrame:textRect];
[addButton setBackgroundColor:[UIColorbrownColor]];
[addButton setTitle:@"="forState:UIControlStateNormal];
[self.viewaddSubview:addButton];
[addButton addTarget:selfaction:@selector(buttonEqual:)forControlEvents:UIControlEventTouchUpInside];
for (int j =0; j < 4; j++) {
NSArray *symbolArray = [NSArrayarrayWithObjects:@"+",@"-", @"*",@"/", nil];
NSString *symbolString = [symbolArray objectAtIndex:j];
textRect.origin.y +=50;
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.origin.y +=50;
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;
if (0 == _calculator) {
inputString = [_inputField.textstringByAppendingString:sender.titleLabel.text];
}else{
inputString = sender.titleLabel.text;
}
_calculator = 0;
[_inputFieldsetText:inputString];
}
- (void)calculateSymbolButton:(UIButton *)sender
{
//此处另一点小瑕疵,须要在推断一下
[selfbuttonEqual:nil];
_firstName = [_inputField.textintegerValue];
//保存输入的下一个数的形式
_calculator = 1;
// 保存计算类型 + - / *(用tag更简单一点)
_buttonType = sender.tag;
}
- (void)clearButton:(UIButton *)sender
{
_firstName = 0;
_calculator = 0;
_buttonType = 0;
_inputField.text =@"";
}
- (void)buttonEqual:(UIButton *)sender
{
NSInteger result;
switch (_buttonType) {
case 0:
result =_firstName + [_inputField.textintegerValue];
break;
case 1:
result =_firstName - [_inputField.textintegerValue];
break;
case 2:
result =_firstName * [_inputField.textintegerValue];
break;
case 3:
result =_firstName / [_inputField.textintegerValue];
break;
default:
break;
}
NSString *end = [NSStringstringWithFormat:@"%ld", result];
_inputField.text = end;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。