@interface MainViewController : UIViewController
@property (nonatomic, retain)UIButton *button;
@property (nonatomic, retain)UILabel *laber;
@property (nonatomic, assign)NSInteger firstNumber;//记录第一个数字的值
@property (nonatomic, assign)bool buttonType;// 是否点击了解符号键, 默认为NO
@property (nonatomic, assign) NSInteger mathType;//0-加 1-减 2- 乘 3-除
@end
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.laber = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 280, 50)];
self.laber.backgroundColor = [UIColor brownColor];
self.laber.text = @"";
self.laber.textColor = [UIColor blackColor];
self.laber.textAlignment = NSTextAlignmentRight;
[self.view addSubview:self.laber];
[self.laber release];
// UIButton *numberButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
// numberButton.frame = CGRectMake(20, 140, 50, 50);
// [numberButton setTitle:@"2" forState:(UIControlStateNormal)];
// [numberButton setBackgroundColor:[UIColor brownColor]];
// [numberButton addTarget:self action:@selector(nuberButtonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
// [self.view addSubview:numberButton];
//添加1-9数字
NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil];
int n=0;
for (int i=0; i<3; i++)
{
for (int j=0; j<3; j++)
{
self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame=CGRectMake(30+65*j, 150+65*i, 60, 60);
[self.button setTitle:[array objectAtIndex:n++] forState:UIControlStateNormal];
[self.view addSubview:self.button];
[self.button addTarget:self action:@selector(nuberButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
}
UIButton *equal1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
equal1.frame = CGRectMake(260, 140, 50, 50);
[equal1 setTitle:@" = " forState:(UIControlStateNormal)];
[equal1 setBackgroundColor:[UIColor brownColor]];
[equal1 addTarget:self action:@selector(equal:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:equal1];
UIButton *add1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
add1.frame = CGRectMake(200, 140, 50, 50);
[add1 setTitle:@" + " forState:(UIControlStateNormal)];
[add1 setBackgroundColor:[UIColor blackColor]];
[add1 addTarget:self action:@selector(add:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:add1];
}
//- (void)dealloc
//{
//
//}
//数字键
- (void)nuberButtonClicked:(UIButton *)button
{
if (self.buttonType == NO) {
//如果没有点击符号键
// 1.获取到当前的数字
NSString *number1 = self.laber.text;
// 2.在当前的数字后面拼接一个2
// self.laber.text =[NSString stringWithFormat:@"%@%@",number1, button.currentTitle];
self.laber.text = [number1 stringByAppendingString:button.currentTitle];
} else{
//如果刚刚点击了符号键
self.laber.text = button.titleLabel.text;
//把符号键状态 取消
self.buttonType = NO;
}
}
//符号键
- (void)add:(UIButton *)button
{
//当点击加号键时 将第一个数字保存
self.firstNumber = [self.laber.text integerValue];
self.buttonType = YES;//已经点击了符号键
self.mathType = 0;// 当前点击的是加号
}
// 等号键
- (void)equal:(UIButton *)button
{
NSInteger secondNumber = [self.laber.text integerValue];
//计算
if (0 == self.mathType) {
NSInteger result = secondNumber + self.firstNumber;
//将结果赋给laber显示
self.laber.text = [NSString stringWithFormat:@"%d", result];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end