Is this appropriate code for the arrays in a picker with two identical components? Or can both components somehow use the same arrays?
- (void)viewDidLoad
{
self.fromArray = [[NSArray alloc] initWithObjects:
@"Annual", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];
self.fromValues = [[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];
self.toArray = [[NSArray alloc] initWithObjects:
@"Annual", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];
self.toValues = [[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];
Also, how would I properly format a calculation using values from both components and a Text Field? This is just not working.
I tried this, but I get undeclared identifier errors with the use of 'row'.
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component == 0) {
return [fromArray objectAtIndex:row];
}
return [toArray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
}
- (IBAction)Convert:(id)sender {
float valuein = [[fromValues objectAtIndex:row] floatValue];
float valueout = [[toValues objectAtIndex:row] floatValue];
float input = [inputText.text floatValue];
float result = input * valuein / valueout;
NSString *resultString = [[NSString alloc] initWithFormat: @"$ %@", [toArray objectAtIndex:row]];
resultText.text = [NSString stringWithFormat:@"$%6.2f",result];
}
EDIT: Here is the current version of full .m file code, as requested below. I suspect there is a bit of unnecessary code. The only warning is Unused variable 'resultString'.
#import "PayFrequencyViewController.h"
@interface PayFrequencyViewController ()
@end
@implementation PayFrequencyViewController
@synthesize payPicker;
@synthesize toArray;
@synthesize fromArray;
@synthesize toValues;
@synthesize fromValues;
@synthesize resultText;
@synthesize inputText;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.fromArray = [[NSArray alloc] initWithObjects:
@"Annual", @"Monthly", @"48/Year", @"SemiMonthly", @"BiWeekly", @"Weekly", @"Hourly", nil];
self.fromValues = [[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];
self.toArray = self.fromArray;
self.toValues = self.fromValues;
[super viewDidLoad];
inputText.keyboardType = UIKeyboardTypeDecimalPad;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.toArray = nil;
self.fromArray = nil;
self.resultText = nil;
self.inputText = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return [fromArray count];
}
return [toArray count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component == 0) {
return [fromArray objectAtIndex:row];
}
return [toArray objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
_row = row;
}
- (IBAction)Convert:(id)sender {
// float valuein = [[fromValues text] floatValue];
// float valueout = [[toValues text] floatValue];
// [resultText setText:[NSString stringWithFormat:@"%6.2f", result]];
// [inputText resignFirstResponder];
float valuein = [[fromValues objectAtIndex:_row] floatValue];
float valueout = [[toValues objectAtIndex:_row] floatValue];
float input = [inputText.text floatValue];
float result = input * valuein / valueout;
NSString *resultString = [[NSString alloc] initWithFormat: @"$ %@", [toArray objectAtIndex:_row]];
resultText.text = [NSString stringWithFormat:@"$%6.2f",result];
}
- (IBAction)Clear:(id)sender {
inputText.text = @"";
resultText.text = @"";
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[inputText resignFirstResponder];
}
@end