本帖最后由 余丽丽 于 2015-8-5 23:12 编辑
实现效果:
具体代码:
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; self.firstNumTextField.delegate=self; self.lastNumTextField.delegate = self;
//加载初始状态 [self.calNumButton setTitle:@"请输入要计算的数据" forState:UIControlStateNormal]; self.addButton.selected=YES; self.selectButton=self.addButton;
NSLog(@"%@",self.firstNumTextField.placeholder); }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];
}
//点击加减乘除等于按钮 - (IBAction)clickCalButtons:(UIButton*)sender { double result = 0.0; switch (sender.tag) {
case kAdd:// + self.firstNumLabel.text = @"被加数"; self.lastNumLabel.text = @"加数"; self.selectButton.selected = NO; self.addButton.selected=YES; self.selectButton=self.addButton; break;
case kSub:// - self.firstNumLabel.text = @"被减数"; self.lastNumLabel.text = @"减数"; self.selectButton.selected = NO; self.subButton.selected=YES; self.selectButton=self.subButton; break;
case kMul:// * self.firstNumLabel.text = @"被乘数"; self.lastNumLabel.text = @"乘数"; self.selectButton.selected = NO; self.mulButton.selected=YES; self.selectButton=self.mulButton; break;
case kDiv:// / self.firstNumLabel.text = @"被除数"; self.lastNumLabel.text = @"除数"; self.selectButton.selected = NO; self.divButton.selected=YES; self.selectButton=self.divButton; break;
case kCal:// = self.firstNumTextField.placeholder=([self.firstNumTextField.text isEqualToString:@""]==true) ? @"您还没有输入数!":nil; self.lastNumTextField.placeholder=([self.lastNumTextField.text isEqualToString:@""]==true) ? @"您还没有输入数!":nil; if ([self.firstNumTextField.placeholder length]==0 && [self.lastNumTextField.placeholder length]==0) { switch (self.selectButton.tag) { case kAdd: result = self.firstNumTextField.text.doubleValue+self.lastNumTextField.text.doubleValue; break; case kSub: result = self.firstNumTextField.text.doubleValue-self.lastNumTextField.text.doubleValue; break; case kMul: result = self.firstNumTextField.text.doubleValue*self.lastNumTextField.text.doubleValue; break; case kDiv: result = self.firstNumTextField.text.doubleValue/self.lastNumTextField.text.doubleValue; break; }
[self.calNumButton setTitle:[NSString stringWithFormat:@"%.0f",result] forState:UIControlStateNormal]; }
break; } }
//点击计算结果的按钮 - (IBAction)clickCalNumButton {
if ([self.calNumButton.titleLabel.text isEqualToString:@"请输入要计算的数据"]) { }else { self.firstNumTextField.text=self.calNumButton.titleLabel.text; [self.calNumButton setTitle:@"请输入要计算的数据" forState:UIControlStateNormal]; self.lastNumTextField.text=nil; } }
//监听空白区域 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES]; }
//监听return -(BOOL)textFieldShouldReturn:(UITextField *)textField { [self.view endEditing:YES]; return YES;
}
@end
|