Ramya sree
Joined: 12 Mar 2010 Posts: 59
|
Posted: Mon Jul 12, 2010 1:33 pm Post subject: Sample Code for Temperature Conversions: |
|
|
Hi to All,
You can observe from the below code, how the temperature can be converted from Celsius to Farenheit Or Vice-versa, basing on User requirement.
>To create a new project, choose view-based template, name it as "TemperatureViewController".
>Open TemperatureViewController.h file , convert it like below:
@interface TemperatureViewController : UIViewController
{
IBOutlet UITextField *temperature;
IBOutlet UILabel *farenheit;
IBOutlet UILabel *degree;
}
@property(nonatomic, retain) UITextField *temperature;
@property(nonatomic, retain) UILabel *farenheit;
@property(nonatomic, retain) UILabel *degree;
-(IBAction)farenheitToCelsius;
-(IBAction)celsiusToFarenheit;
@end
>Then,implement the methods of .h file in TemperatureViewController.m file as :
>Synthesize all the properties here.
// defining the method for converting farenheit temperature into celsius..
-(IBAction)farenheitToCelsius
{
NSString *toCelsius = [temperature text];
double ce = (5.0/9.0)*([toCelsius doubleValue]-32.0);
NSString *cels =@"Temperature in Celsius is";
farenheit.text = [NSString stringWithFormat:@"%@: %2.0f", cels, ce];
}
//code for converting celsius temperature into farenheit
-(IBAction)celsiusToFarenheit
{
NSString *toFarenheit = [temperature text];
double fa = (9.0/5.0)*([toFarenheit doubleValue]+32.0);
NSString *faren =@"Farenheit Temperature is";
farenheit.text = [NSString stringWithFormat:@"%@: %0.4f", faren, fa];
[degree setText:@"˚F"];
}
> Open .xib file for InterfaceBuilder connections..Drag-n-drop the required objects on to the view panel,
to match with the variables defined in TemperatureViewController.h file & make the required connections.
Thank You,
Ramya  |
|