How to code calculations in Xcode using 2-component Picker and Text field entry
===

I am attempting to code an iPhone/iPad screen that outputs a value to a text field based on what is entered in another text field and what is selected in a 2-component picker. I have the basics done, right up to the point of having the entered/selected data appear, but short of performing the calculation. I have searched every resource I can find and have made use of all the relevant code I found, as you will see below.

Specifically, I want to allow the user to enter a dollar amount in one text field, select what pay frequency (hourly, monthly, etc) it is, select what pay frequency they would like to convert it to, and see the results in the other text field when they tap a button. Another button would clear the fields, so they can convert another amount. More specifically, if they wanted to see what their weekly wage of $310 was semimonthly, they would enter "310" in the first field, select "Weekly" in the first picker component, select "SemiMonthly" in the other component, and tap the Calculate button. The other field would then show $671.67 (310 * 52 / 24 = 671.67).

Just in case it is relevant, the View Controller this takes place on is tied to a Navigation Controller as one of 5 screens attached to one of 6 screens attached to the main View Controller (it takes a couple of taps to get to this screen).

So far, I can enter numbers into the two text fields and I can spin the 2 picker components and make their results appear in a label. I also have what I believe to be the correct calculation, but that's where I'm stuck. I can find no examples of how to implement it.



Here is my ViewController.m code:

> #import <UIKit/UIKit.h>
> 
> @interface PayFrequencyViewController : UIViewController
> <UIPickerViewDelegate, UIPickerViewDataSource> {
>     UIPickerView *payPicker;
>     NSArray *pay1Array;
>     NSArray *pay2Array;
>     NSArray *conversionRates;
>     UILabel *pay1Label;
>     UILabel *pay2Label;
> 
> } @property (nonatomic, retain) IBOutlet UIPickerView *payPicker;
> @property (nonatomic, retain) NSArray *pay1Array; @property
> (nonatomic, retain) NSArray *pay2Array; @property (nonatomic, retain)
> NSArray *conversionRates; @property (nonatomic, retain) IBOutlet
> UILabel *pay1Label; @property (nonatomic, retain) IBOutlet UILabel
> *pay2Label; @property (nonatomic, retain) IBOutlet UILabel *result; @end



And here is my ViewController.m code:

> #import "PayFrequencyViewController.h"
> 
> @interface PayFrequencyViewController ()
> 
> @end
> 
> @implementation PayFrequencyViewController @synthesize payPicker,
> pay2Array, pay1Array; @synthesize pay2Label, pay1Label,
> conversionRates, result;
> 
> 
> - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
>     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
>     if (self) {
>         // Custom initialization
>     }
>     return self; }
> 
> - (void)viewDidLoad {
>     self.pay1Array = [[NSArray alloc] initWithObjects:
>                       @"Annually", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];
>     
>     self.pay2Array = [[NSArray alloc] initWithObjects:
>                       @"Annually", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];
> 
>     self.conversionRates = [[NSArray alloc] initWithObjects: [NSNumber numberWithFloat:1.0], [NSNumber numberWithFloat:12.0], [NSNumber
> numberWithFloat:48.0], [NSNumber numberWithFloat:24.0], [NSNumber
> numberWithFloat:26.0], [NSNumber numberWithFloat:52.0], [NSNumber
> numberWithFloat:2080.0], nil];
>     [super viewDidLoad]; 	// Do any additional setup after loading the view. }
> 
> - (void)viewDidUnload {
>     // Release any retained subviews of the main view.
>     // e.g. self.myOutlet = nil;
>     self.payPicker = nil;
>     self.pay1Label = nil;
>     self.pay2Label = nil; }
> 
> - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
> { 	return YES; }
> 
> #pragma mark -
> #pragma mark PickerView DataSource
> 
> - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
>     
>     return 2; }
> 
> - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
>     
>     if (component == 0) {
>         return [pay1Array count];
>     }
>     return [pay2Array count]; }
> 
> - (NSString *)pickerView:(UIPickerView *)pickerView
>              titleForRow:(NSInteger)row
>             forComponent:(NSInteger)component {
>     if (component == 0) {
>         return [pay1Array objectAtIndex:row];
>     }
>     return [pay2Array objectAtIndex:row]; } 
> 
> #pragma mark -
> #pragma mark PickerView Delegate
> 
> -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
>       inComponent:(NSInteger)component {
>     float rate = [[conversionRates objectAtIndex:row] floatValue];
>     float dollars1 = [pay1Label.text floatValue];
>     float dollars2 = [pay2Label.text floatValue];
>     // float result = dollars1 * rate1 / rate2;
>     
>     if (component == 0)
>     {
>         NSString *resultString = [[NSString alloc] initWithFormat:
>                                   @"from:  %@",
>                                   [pay1Array objectAtIndex:row]];
>         pay1Label.text = resultString;
>     } else {
>         NSString *resultString = [[NSString alloc] initWithFormat:
>                                   @"to:  %@",
>                                   [pay2Array objectAtIndex:row]];
>         pay2Label.text = resultString;
>     }     }
> 
> -(IBAction)textFieldReturn:(id)sender {
>     [sender resignFirstResponder]; }    
> 
> @end

My apologies for the lengthy post. I hope someone can tell me what code is unnecessary and what is required to complete this task.