UILabel *plusLable = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 200, 40)];
plusLable.backgroundColor = [UIColor whiteColor];
plusLable.textAlignment = NSTextAlignmentRight; //所显示的文本内容位置
plusLable.tag = 100;
[self.window addSubview:plusLable];
[plusLable release];
UIButton *a1Button= [UIButton buttonWithType: UIButtonTypeRoundedRect];
a1Button.frame = CGRectMake(40, 100, 40, 40);
[a1Button setTitle:@"1" forState:UIControlStateNormal];
a1Button.backgroundColor = [UIColor blackColor];
[a1Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a1Button];
UIButton *a2Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a2Button.frame = CGRectMake(120, 100, 40, 40);
[a2Button setTitle:@"2" forState:UIControlStateNormal];
a2Button.backgroundColor = [UIColor blackColor];
[a2Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a2Button];
UIButton *a3Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a3Button.frame = CGRectMake(200, 100, 40, 40);
[a3Button setTitle:@"3" forState:UIControlStateNormal];
a3Button.backgroundColor = [UIColor blackColor];
[a3Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a3Button];
UIButton *a4Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a4Button.frame = CGRectMake(40,160, 40, 40);
[a4Button setTitle:@"4" forState:UIControlStateNormal];
a4Button.backgroundColor = [UIColor blackColor];
[a4Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a4Button];
UIButton *a5Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a5Button.frame = CGRectMake(120, 160, 40, 40);
[a5Button setTitle:@"5" forState:UIControlStateNormal];
a5Button.backgroundColor = [UIColor blackColor];
[a5Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a5Button];
UIButton*a6Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a6Button.frame = CGRectMake(200, 160, 40, 40);
[a6Button setTitle:@"6" forState:UIControlStateNormal];
a6Button.backgroundColor = [UIColor blackColor];
[a6Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a6Button];
UIButton *a7Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a7Button.frame = CGRectMake(40,220, 40, 40);
[a7Button setTitle:@"7" forState:UIControlStateNormal];
a7Button.backgroundColor = [UIColor blackColor];
[a7Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a7Button];
UIButton *a8Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a8Button.frame = CGRectMake(120, 220, 40, 40);
[a8Button setTitle:@"8" forState:UIControlStateNormal];
a8Button.backgroundColor = [UIColor blackColor];
[a8Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a8Button];
UIButton * a9Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a9Button.frame = CGRectMake(200, 220, 40, 40);
[a9Button setTitle:@"9" forState:UIControlStateNormal];
a9Button.backgroundColor = [UIColor blackColor];
[a9Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a9Button];
UIButton *dyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dyButton.frame = CGRectMake(260, 100, 40, 40);
[dyButton setTitle:@"=" forState:UIControlStateNormal];
dyButton.backgroundColor = [UIColor blackColor];
[dyButton addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:dyButton];
UIButton *sumButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
sumButton.frame = CGRectMake(260, 160, 40, 40);
[sumButton setTitle:@"+" forState:UIControlStateNormal];
sumButton.backgroundColor = [UIColor blackColor];
[sumButton addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:sumButton];
UIButton *jfButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
jfButton.frame = CGRectMake(260, 220, 40, 40);
[jfButton setTitle:@"-" forState:UIControlStateNormal];
jfButton.backgroundColor = [UIColor blackColor];
[sumButton addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:jfButton];
return YES;
}
- (void)plus1:(UIButton *)button
{
int sum = 0 ;
NSString *number1 =[button titleForState:UIControlStateNormal];
UILabel *lable= (UILabel *)[self.window viewWithTag:100];
int number = number1.intValue;
if (number >= 0 && number <= 9) {
lable.text = number1 ;
}
if ([number1 isEqualToString:@"+"]) {
sum += number;
}
if ([number1 isEqualToString:@"="]) {
lable.text = [NSString stringWithFormat:@"%d", sum];
}
}
plusLable.backgroundColor = [UIColor whiteColor];
plusLable.textAlignment = NSTextAlignmentRight; //所显示的文本内容位置
plusLable.tag = 100;
[self.window addSubview:plusLable];
[plusLable release];
UIButton *a1Button= [UIButton buttonWithType: UIButtonTypeRoundedRect];
a1Button.frame = CGRectMake(40, 100, 40, 40);
[a1Button setTitle:@"1" forState:UIControlStateNormal];
a1Button.backgroundColor = [UIColor blackColor];
[a1Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a1Button];
UIButton *a2Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a2Button.frame = CGRectMake(120, 100, 40, 40);
[a2Button setTitle:@"2" forState:UIControlStateNormal];
a2Button.backgroundColor = [UIColor blackColor];
[a2Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a2Button];
UIButton *a3Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a3Button.frame = CGRectMake(200, 100, 40, 40);
[a3Button setTitle:@"3" forState:UIControlStateNormal];
a3Button.backgroundColor = [UIColor blackColor];
[a3Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a3Button];
UIButton *a4Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a4Button.frame = CGRectMake(40,160, 40, 40);
[a4Button setTitle:@"4" forState:UIControlStateNormal];
a4Button.backgroundColor = [UIColor blackColor];
[a4Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a4Button];
UIButton *a5Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a5Button.frame = CGRectMake(120, 160, 40, 40);
[a5Button setTitle:@"5" forState:UIControlStateNormal];
a5Button.backgroundColor = [UIColor blackColor];
[a5Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a5Button];
UIButton*a6Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a6Button.frame = CGRectMake(200, 160, 40, 40);
[a6Button setTitle:@"6" forState:UIControlStateNormal];
a6Button.backgroundColor = [UIColor blackColor];
[a6Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a6Button];
UIButton *a7Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a7Button.frame = CGRectMake(40,220, 40, 40);
[a7Button setTitle:@"7" forState:UIControlStateNormal];
a7Button.backgroundColor = [UIColor blackColor];
[a7Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a7Button];
UIButton *a8Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a8Button.frame = CGRectMake(120, 220, 40, 40);
[a8Button setTitle:@"8" forState:UIControlStateNormal];
a8Button.backgroundColor = [UIColor blackColor];
[a8Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a8Button];
UIButton * a9Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
a9Button.frame = CGRectMake(200, 220, 40, 40);
[a9Button setTitle:@"9" forState:UIControlStateNormal];
a9Button.backgroundColor = [UIColor blackColor];
[a9Button addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:a9Button];
UIButton *dyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
dyButton.frame = CGRectMake(260, 100, 40, 40);
[dyButton setTitle:@"=" forState:UIControlStateNormal];
dyButton.backgroundColor = [UIColor blackColor];
[dyButton addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:dyButton];
UIButton *sumButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
sumButton.frame = CGRectMake(260, 160, 40, 40);
[sumButton setTitle:@"+" forState:UIControlStateNormal];
sumButton.backgroundColor = [UIColor blackColor];
[sumButton addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:sumButton];
UIButton *jfButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
jfButton.frame = CGRectMake(260, 220, 40, 40);
[jfButton setTitle:@"-" forState:UIControlStateNormal];
jfButton.backgroundColor = [UIColor blackColor];
[sumButton addTarget:self action:@selector(plus1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:jfButton];
return YES;
}
- (void)plus1:(UIButton *)button
{
int sum = 0 ;
NSString *number1 =[button titleForState:UIControlStateNormal];
UILabel *lable= (UILabel *)[self.window viewWithTag:100];
int number = number1.intValue;
if (number >= 0 && number <= 9) {
lable.text = number1 ;
}
if ([number1 isEqualToString:@"+"]) {
sum += number;
}
if ([number1 isEqualToString:@"="]) {
lable.text = [NSString stringWithFormat:@"%d", sum];
}
}