Advertisement
Guest User

BMICalculatorViewController.m

a guest
Jul 10th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "BMICalculatorViewController.h"
  2.  
  3. @interface BMICalculatorViewController ()
  4.  
  5. @property (weak, nonatomic) IBOutlet UITextField *height_TextField;
  6. @property (weak, nonatomic) IBOutlet UITextField *weight_TextField;
  7. @property (weak, nonatomic) IBOutlet UILabel *resultLabel;
  8. - (IBAction)calculateBMI:(id)sender;
  9.  
  10. @end
  11.  
  12. @implementation BMICalculatorViewController
  13.  
  14. - (void)viewDidLoad
  15. {
  16.     [super viewDidLoad];
  17.     // Do any additional setup after loading the view, typically from a nib.
  18.    
  19.     // Gesture for tap anywhere to dissmiss keyboard.
  20.     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
  21.     [self.view addGestureRecognizer:tap];
  22. }
  23.  
  24. - (void)didReceiveMemoryWarning
  25. {
  26.     [super didReceiveMemoryWarning];
  27.     // Dispose of any resources that can be recreated.
  28. }
  29.  
  30. - (void)dismissKeyboard
  31. { // Method for tap anywhere to dismiss keyboard
  32.     [_height_TextField resignFirstResponder];
  33.     [_weight_TextField resignFirstResponder];
  34. }
  35.  
  36. - (IBAction)calculateBMI:(id)sender {
  37.     // BMI formula: BMI = ( Weight(Kg) / ( Height(M) x Height(M) ) )
  38.     double height = [self.height_TextField.text doubleValue];
  39.     double weight = [self.weight_TextField.text doubleValue];
  40.    
  41.     NSString *bmi = [NSString stringWithFormat:@"%.2lf", (weight/(height*height))];
  42.     NSString *resultString = [NSString stringWithFormat:@"Your BMI is:\n%@", bmi];
  43.    
  44.     // Declaring UIAlertView *resultAlert
  45.     UIAlertView *resultAlert = [[UIAlertView alloc] initWithTitle:@"Invalid"
  46.                                                           message:nil
  47.                                                          delegate:self
  48.                                                 cancelButtonTitle:@"Okay"
  49.                                                 otherButtonTitles:nil];
  50.    
  51.     if ([self.weight_TextField.text length] == 0 && [self.height_TextField.text length] == 0)
  52.     {
  53.         resultString = @"Please fill in your Height and Weight!";
  54.         resultAlert.message = resultString; // Update resultString with error message.
  55.         [resultAlert show];
  56.     }
  57.     else if ([self.height_TextField.text length] == 0)
  58.     {
  59.         resultString = @"Please fill in your Height!";
  60.         resultAlert.message = resultString;
  61.         [resultAlert show];
  62.        
  63.        
  64.     }
  65.     else if ([self.weight_TextField.text length] == 0)
  66.     {
  67.         resultString = @"Please fill in your Weight!";
  68.         resultAlert.message = resultString;
  69.         [resultAlert show];
  70.     }
  71.     else
  72.     {
  73.         self.resultLabel.text = resultString;
  74.        
  75.         // Clear text fields after results are showned.
  76.         self.height_TextField.text = nil;
  77.         self.weight_TextField.text = nil;
  78.     }
  79. }
  80.  
  81. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  82. {
  83.     [textField resignFirstResponder];
  84.     return YES;
  85. }
  86.  
  87. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  88. {
  89.    
  90. }
  91.  
  92. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement