// // EditModelPriceViewController.m // RedAnt ERP Mobile // // Created by Ray on 9/16/15. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved. // #import "EditModelPriceViewController.h" #import "RAUtils.h" #import "RANetwork.h" #define NUMBERS @"0123456789.\n" @interface EditModelPriceViewController () @end @implementation EditModelPriceViewController - (void)viewDidLoad { [super viewDidLoad]; self.editPrice.text = [NSString stringWithFormat:@"%.2f", self.price ]; self.editDiscount.text = [NSString stringWithFormat:@"%@", [RAUtils FloatFormat:self.discount] ]; self.labelNewPrice.text = [NSString stringWithFormat:@"%.2f",self.price* (1.0-self.discount/100)]; if(self.hide_discount) { self.editDiscount.hidden=true; self.labelDiscount.hidden=true; self.labelCalPrice.text = @"New price:"; } // Do any additional setup after loading the view. } - (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. } */ - (IBAction)onCloseClicked:(id)sender { [self dismissViewControllerAnimated:NO completion:^{ }]; } - (IBAction)onSaveClicked:(id)sender { if([self.editDiscount.text floatValue]>100.0) { [RAUtils message_box:@"Some Requried Fields Are Missing." message:@"Fields with * mark cannot be empty." completion:nil]; self.editDiscount.text=@"0"; return; } [self update_newprice]; [self dismissViewControllerAnimated:NO completion:^{ if(self.onSetValue) self.onSetValue( self.price,self.discount); }]; } #pragma mark textField delegate - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; } -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ if(textField.text.length==0 && [string isEqualToString:@"."]) return false; NSCharacterSet *cs; cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet]; // NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; BOOL canChange = [string isEqualToString:filtered]; // // // // return canChange; // return true; } -(void) update_newprice { self.price = [self.editPrice.text doubleValue]; self.discount = [self.editDiscount.text doubleValue]; self.labelNewPrice.text = [NSString stringWithFormat:@"%.2f",self.price* (1.0-self.discount/100)]; } - (void)textFieldDidEndEditing:(UITextField *)textField { if(textField.tag==2) { if(textField.text.length==0) textField.text=@"0"; if([textField.text floatValue]>100.0) { [RAUtils message_box:@"Input Error." message:@"Discount must less than 100." completion:nil]; textField.text=@"0"; } } else { float f = [textField.text floatValue]; textField.text=[NSString stringWithFormat:@"%.2f",f]; } [self update_newprice]; } - (void)textFieldDidBeginEditing:(UITextField *)textField { // // self.lastedit = textField; } @end