Sunday, July 3, 2011

iPhone application tutorial - Body Mass Index Calculator : Part 6



This is our last part of the tutorial. We have done whatever that needs to be done with respect to the UI. All that is left is the coding part. In the last post, you would have seen that we had a couple of methods defined called as updateResult() and range(). These are supposed to be private methods. But then, I don't want to overload the readers with that concept yet, hence I have not set the access modifier for them. They will come in handy in our implementation part.

The implementation part involves responding to the actions that are sent from the UI. Lets take a look at the implementation of the height slider and go line by line.

- (IBAction)heightSlider:(id)sender {
    UISlider *hSlider = (UISlider*) sender;
 int hVal = (int)hSlider.value;
 NSString *hLabel = [[NSString alloc]initWithFormat:@"%i", hVal];
 height.text = hLabel;
 [self updateResults];
 [hLabel release];
}

The first line is nothing but a copy of the method declaration. The return type of the this method is an IBAction. The argument that it takes is the sender's object. If you had known Java, 'id' is a data type similar to Object. It is the sender's object. Since we have attached this method with the Slider, this would be the Slider itself.

On the fist line inside the method, we are typecasting the sender to the UISlider since we know for sure that the sender is an instance of UISlider. We then get its value, asssign it to a NSString variable followed by assigning it to the height text. Since we have allocated the NSString we should release it. You could notice there is a tiny little call to updateResults. It is nothing but updating the BMI and the Result at the bottom. Its implemenation is,

- (void)updateResults{
    float h = [height.text floatValue];
 float w = [weight.text floatValue];
 float bmiVal = w / ((h/100)*(h/100));
 NSString *bmiResult = [[NSString alloc]initWithFormat:@"%.1f", bmiVal];
 bmi.text = bmiResult;
 result.text = [self range:bmiVal];
 [bmiResult release];
}

- (NSString *) range:(float)bmiVal{
    NSString *theRange;
 
 if(bmiVal < 16.5){
  theRange = @"Severly Underweight";
 }else if (bmiVal<18.5) {
  theRange = @"Underweight";
 }else if (bmiVal<25) {
  theRange = @"Normal";
 }else if (bmiVal<30) {
  theRange = @"Overweight";
 }else if (bmiVal<35) {
  theRange = @"Obese";
 }else {
  theRange = @"Severly Obese";
 }
 return theRange;
}

The above code is self explanatory and straightforward to read. We also need to add a similar slider method for weight as we did for height.

- (IBAction)weightSlider:(id)sender {
    UISlider *wSlider = (UISlider*) sender;
 int wVal = (int)wSlider.value;
 NSString *wLabel = [[NSString alloc]initWithFormat:@"%i", wVal];
 weight.text = wLabel;
 [self updateResults];
 [wLabel release];
}

Whoof thats a lotta coding. Lets see the final output in the final part of this tutorial!

Cheers!
Braga

2 comments:

santa said...

Body weight of a person is theoretically the weight of the person without any items on. However, for all practical purposes, body weight is taken with clothes on but often without the shoes and heavy accessories like mobile phones and wallets. Thanks.
Regards,
hcg1234.com

santa said...

The body mass Index is a great way to find out if you are underweight , overweight , or if you are at your ideal weight for your height and age.
Thanks a lot.
Regards,
how to gain weight