Time Converter Application in iPhone

This is the Time Converter application. Using this application we can can convert time like, Hour to Minute, Hour to Second. So let see how it will worked. My previous topic reference you can find out from here PickerView Programmatically

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “PickerView_usingButtonPress”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add images in the resource folder.

Step 4: Open the PickerView_usingButtonPress.h file and make the following changes:

#import <UIKit/UIKit.h>

@interface PickerView_UsingButtonPressViewController : UIViewController {

NSMutableArray *array_from;
NSMutableArray *array_to;
UIPickerView *fromPickerView;
UIPickerView *toPickerView;
UIButton *doneButton ;
UITextField *fromButton;
UIButton *button1;

UIImageView *m_objBackgroundView;

UITextField *toTextButton;
UIButton *toButton;

IBOutlet UILabel*resultLabel;
IBOutlet UITextField *valueData;
IBOutlet UILabel*UnitLabel;

}
@property(nonatomic,retain) IBOutlet UITextField *fromButton;
@property(nonatomic,retain) IBOutlet   UIButton *button1;
@property(nonatomic,retain) IBOutlet UITextField *toTextButton;
@property(nonatomic,retain) IBOutlet   UIButton *toButton;

(IBAction)FromButton:(id)sender;
(IBAction)ToButton:(id)sender;
(IBAction)Calculation:(id)sender;

@end

Step 5: Double click the PickerView_usingButtonPress.xib file and open it to the Interface Builder. First drag the three label from the library and place it to the view window. Give the label name Value:, From: and To: . And drag the three  TextField from the library and place it to the view window. Connect Files Owner icon to the textfield and select valueData, fromButton and toTextButton. Now drag the Round Rect Button from the library and place it to the view window. Select the Round Rect Button and bring up Connection Inspector and connect Touch Up inside to the File’s Owner icon and select Calculation: method. Now save the .xib file, close it and go back to the Xcode.

Step 6: In the PickerView_usingButtonPress.m file and make the following changes:

#import "PickerView_UsingButtonPressViewController.h"

#define kPICKERCOLUMN 1
#define kFROMPICKERTAG 20
#define kTOPICKERTAG 21

@implementation PickerView_UsingButtonPressViewController

@synthesize fromButton,button1,toTextButton,toButton;

(IBAction)ToButton:(id)sender
{
 
    toPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    toPickerView.tag = kTOPICKERTAG;
   
    [self.view addSubview:toPickerView];
    toPickerView.delegate = self;
    toPickerView.showsSelectionIndicator = YES;
   
    doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [doneButton addTarget:self
                   action:@selector(ToPickerViewRemove:)
         forControlEvents:UIControlEventTouchDown];
    doneButton.frame = CGRectMake(265.0,202.0,  52.0, 30.0);
    UIImage *img = [UIImage imageNamed:@"done.png"];
    [doneButton setImage:img forState:UIControlStateNormal];
   
    [img release];
   
    [self.view addSubview:doneButton];
   
     
}

(IBAction)FromButton:(id)sender
{
    fromPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    fromPickerView.tag = kFROMPICKERTAG;
    [self.view addSubview:fromPickerView];
    fromPickerView.delegate = self;
    fromPickerView.showsSelectionIndicator = YES;
   
    doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [doneButton addTarget:self
                   action:@selector(FromPickerViewRemove:)
         forControlEvents:UIControlEventTouchDown];
    doneButton.frame = CGRectMake(265.0,202.0,  52.0, 30.0);
    UIImage *img = [UIImage imageNamed:@"done.png"];
    [doneButton setImage:img forState:UIControlStateNormal];
   
    [img release];
   
    [self.view addSubview:doneButton];
 
}

(IBAction)ToPickerViewRemove:(id)sender
{
    [toPickerView removeFromSuperview];
    [doneButton removeFromSuperview];
    [m_objBackgroundView removeFromSuperview];
}

(IBAction)FromPickerViewRemove:(id)sender
{
    [fromPickerView removeFromSuperview];
    [doneButton removeFromSuperview];
    [m_objBackgroundView removeFromSuperview];
}

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

(void)dealloc
{
    [super dealloc];
}

(void)didReceiveMemoryWarning
{
    // Releases the view if it doesn’t have a superview.
    [super didReceiveMemoryWarning];
   
    // Release any cached data, images, etc that aren’t in use.
}

#pragma mark – View lifecycle

(void)viewDidLoad
{
    [super viewDidLoad];
   
    array_from=[[NSMutableArray alloc]initWithObjects:@"Hour",nil];
   array_to=[[NSMutableArray alloc]initWithObjects:@"Hour",@"Minute",@"Second",nil];

    [super viewDidLoad];
   
 
}

(BOOL)textFieldShouldReturn:(UITextField *)textField{
        [textField resignFirstResponder];
        return YES;
}

(void)viewDidUnload
{
    [super viewDidUnload];
   
}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
   
    if (pickerView.tag == kFROMPICKERTAG)
        return [array_from count];
    else
        return [array_to count];
}

(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return kPICKERCOLUMN;
}

(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   
   
    if (pickerView.tag == kFROMPICKERTAG){
             
        [fromButton setText:[NSString stringWithFormat:@"%@",[array_from objectAtIndex:[pickerView selectedRowInComponent:0]]]];
       
       
    }
    else{
       
         [toTextButton setText:[NSString stringWithFormat:@"%@",[array_to objectAtIndex:[pickerView selectedRowInComponent:0]]]];
    }
   
}

(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView.tag == kFROMPICKERTAG){    
               
        return [array_from objectAtIndex:row];
      }
       
    else{
               
        return [array_to objectAtIndex:row];
    }
     
}

(IBAction)Calculation:(id)sender{
   
    NSString *tempString=fromButton.text;
    NSString *tempString1=toTextButton.text;
   
    NSString *string=@"Hour";
    NSString *string1=@"Minute";
    NSString *string2=@"Second";
   
   
    // From Hour To Hour
   
    if([tempString isEqualToString:string] && [tempString1 isEqualToString:string]){
       
        resultLabel = [[UILabel alloc]initWithFrame:CGRectMake(104, 300, 187, 20)];
        resultLabel.text =  valueData.text;
        [self.view addSubview:resultLabel];
        UnitLabel = [[UILabel alloc]initWithFrame:CGRectMake(104, 321, 187, 20)];
        UnitLabel.text = @"hour";
        [self.view addSubview:UnitLabel];
    }
   
    // From Hour To Minute
   
    if([tempString isEqualToString:string] && [tempString1 isEqualToString:string1]){
        int Result;
        NSString *str;
        int x = [valueData.text intValue];
        Result = (60*x);
        str = [NSString stringWithFormat:@"%d", Result];
        resultLabel = [[UILabel alloc]initWithFrame:CGRectMake(104, 300, 187, 20)];
        resultLabel.text =  str;
        [self.view addSubview:resultLabel];
       
        UnitLabel = [[UILabel alloc]initWithFrame:CGRectMake(104, 321, 187, 20)];
        UnitLabel.text = @"minute";
        [self.view addSubview:UnitLabel];
       
    }
   
    //  From Hour To Second
   
    if([tempString isEqualToString:string] && [tempString1 isEqualToString:string2]){
        int Result;
        int x = [valueData.text intValue];
        Result = (60*60*x);
        NSString *str;
        str = [NSString stringWithFormat:@"%d", Result];
        resultLabel = [[UILabel alloc]initWithFrame:CGRectMake(104, 300, 187, 20)];
        resultLabel.text =  str ;
        [self.view addSubview:resultLabel];
       
        UnitLabel = [[UILabel alloc]initWithFrame:CGRectMake(104, 321, 187, 20)];
        UnitLabel.text = @"second";
        [self.view addSubview:UnitLabel];
     
    }
   
}

// tell the picker the width of each row for a given component
(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    CGFloat componentWidth = 0.0;
        componentWidth = 135.0; // second column is narrower to show numbers
       
        return componentWidth;
}

@end

Step 7: Now Compile and run the application on the Simulator.

You can Download SourceCode from here PickerView_UsingButtonPress

Leave a Reply

Your email address will not be published. Required fields are marked *