问题
I am attempting to code an iPhone/iPad screen that outputs a value based on what is entered in a 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 selections appear in labels, but that's where I'm stuck. I can find no examples of how to implement it.
Here is my ViewController.h code:
@interface PayFrequencyViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
{
UIPickerView *payPicker;
NSArray *fromArray;
NSArray *toArray;
NSArray *fromValues;
NSArray *toValues;
UILabel *resultText;
UITextField *inputText;
}
@property (strong, nonatomic) IBOutlet UIPickerView *payPicker;
@property (strong, nonatomic) NSArray *fromArray;
@property (strong, nonatomic) NSArray *toArray;
@property (strong, nonatomic) NSArray *fromValues;
@property (strong, nonatomic) NSArray *toValues;
@property (strong, nonatomic) IBOutlet UILabel *resultText;
@property (strong, nonatomic) IBOutlet UITextField *inputText;
- (IBAction)Convert:(id)sender;
- (IBAction)Clear:(id)sender;
@end
And here is my ViewController.m code:
@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 = [[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];
[super viewDidLoad];
inputText.keyboardType = UIKeyboardTypeDecimalPad;
// Do any additional setup after loading the view.
}
- (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 {
//- (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]];
}
- (IBAction)Clear:(id)sender {
inputText.text = @"";
resultText.text = @"";
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[inputText resignFirstResponder];
}
@end
My apologies for the lengthy post and the improperly formatted code. I hope someone can tell me what code is unnecessary and what is required to complete this task.
来源:https://stackoverflow.com/questions/11546164/how-to-code-calculations-in-xcode-using-2-component-picker-and-text-field-entry