我正在尝试为一些单位的东西制作一个简单的转换器,我似乎无法管理从两列中获取数据并将它们变成我可以计算的变量。我也遇到了一些错误,如果有人愿意调查,我将不胜感激。错误出现在#pragma 标记处 - PickerView Delegate,我认为我的代码中有 90% 是错误的。
这是完整的 .h 文件。(希望这次我做对了)如果有人有时间研究它,这里是完整的项目。http://www.mediafire.com/download.php?ss706o783ioa59u
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
@synthesize _convertFrom, _convertTo, _convertRates;
@synthesize dollarText, picker, resultLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_convertFrom = @[@"Australia (AUD)", @"China (CNY)",
@"France (EUR)", @"Great Britain (GBP)", @"Japan (JPY)"];
_convertRates = @[ @0.9922f, @6.5938f, @0.7270f,
@0.6206f, @81.57f];
_convertTo = @[@"Australia (AUD)", @"China (CNY)",
@"France (EUR)", @"Great Britain (GBP)", @"Japan (JPY)"];
_convertRates = @[ @0.9922f, @6.5938f, @0.7270f,
@0.6206f, @81.57f];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
//dont forget to put something in here later
}
- (IBAction)textFieldReturn:(id)sender {
[sender resignFirstResponder];
}
#pragma mark -
#pragma mark PickerView DataSource
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if (component == 0) {
return [_convertFrom count];
}
return [_convertTo count];
}
- (NSString *) pickerView: (UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component
{
if (component == 0) {
return [_convertFrom objectAtIndex:[pickerView selectedRowInComponent:0]];
}
return [_convertTo objectAtIndex:[pickerView selectedRowInComponent:1]];
}
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
{
float from = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0] floatValue]];
float to = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1] floatValue]];
float input = [dollarText.text floatValue];
// This stuff is from some guide.
//float rate = [_convertRates[row] floatValue];
//float dollars = [dollarText.text floatValue];
//float result = dollars * rate;
NSString *resultString = [[NSString alloc] initWithFormat:
@"%.2f @% = %.2f %@", input, [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0] floatValue]], result, [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0] floatValue]]];
resultLabel.text = resultString;
}
#pragma mark - Flipside View
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showAlternate"]) {
[[segue destinationViewController] setDelegate:self];
}
}
@end
提前致谢 :)