//计算器加减乘除(代码版)
#import "ViewController.h"
@interface ViewController ()
{
//设置全局变量
UITextField *_textField1;
UITextField *_textField2;
UITextField *_textField3;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//隐藏导航栏
self.navigationController.navigationBarHidden=YES;
//第1个输入框
_textField1=[[UITextField alloc]init];
_textField1.frame=CGRectMake(0, 20, 80, 30);
_textField1.backgroundColor=[UIColor yellowColor];
[self.view addSubview:_textField1];
//第2个输入框
_textField2=[[UITextField alloc]init];
_textField2.frame=CGRectMake(100, 20, 80, 30);
_textField2.backgroundColor=[UIColor redColor];
[self.view addSubview:_textField2];
//第3个输入框
_textField3=[[UITextField alloc]init];
_textField3.frame=CGRectMake(200, 20, 80, 30);
_textField3.backgroundColor=[UIColor blueColor];
[self.view addSubview:_textField3];
//加法绑定方法
UIButton *jiaButton=[[UIButton alloc]init];
jiaButton.frame=CGRectMake(0, 100, 50, 50);
[jiaButton setTitle:@"加法" forState:UIControlStateNormal];
[jiaButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[jiaButton addTarget:self action:@selector(addChange) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:jiaButton];
//减法绑定方法
UIButton *jianButton=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
[jianButton setTitle:@"减法" forState:UIControlStateNormal];
[jianButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[jianButton addTarget:self action:@selector(jianChange) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:jianButton];
//乘法绑定方法
UIButton *chenButton=[[UIButton alloc]initWithFrame:CGRectMake(200, 100, 50, 50)];
[chenButton setTitle:@"乘法" forState:UIControlStateNormal];
[chenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[chenButton addTarget:self action:@selector(chenChange) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:chenButton];
//除法绑定方法
UIButton *chuButton=[[UIButton alloc]initWithFrame:CGRectMake(300, 100, 50, 50)];
[chuButton setTitle:@"除法" forState:UIControlStateNormal];
[chuButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[chuButton addTarget:self action:@selector(chuChange) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:chuButton];
}
-(void)addChange{
_textField3.text=[NSString stringWithFormat:@"%.1f",_textField1.text.floatValue+_textField2.text.floatValue];
}
-(void)jianChange{
_textField3.text=[NSString stringWithFormat:@"%.1f",_textField1.text.floatValue-_textField2.text.floatValue];
}
-(void)chenChange{
_textField3.text=[NSString stringWithFormat:@"%.1f",_textField1.text.floatValue*_textField2.text.floatValue];
}
-(void)chuChange{
_textField3.text=[NSString stringWithFormat:@"%.1f",_textField1.text.floatValue/_textField2.text.floatValue];
}