0

我正在尝试为一些单位的东西制作一个简单的转换器,我似乎无法管理从两列中获取数据并将它们变成我可以计算的变量。我也遇到了一些错误,如果有人愿意调查,我将不胜感激。错误出现在#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

提前致谢 :)

4

2 回答 2

1

我刚刚看了你的代码,但对你的计算仍有一些疑问,但你的大括号是错误的,请只像这样实现它:

-(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 = from;
    float dollars = input;
    float result = dollars * rate;

    NSString *resultString = [[NSString alloc] initWithFormat:
                              @"result = %f = %f * %f", result, dollars, rate];
    resultLabel.text = resultString;
}

-(IBAction)清除{

}

于 2012-10-12T18:47:32.380 回答
0

这两行:

float from = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]     floatValue]];
float to = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1] floatValue]];

应该:

float from = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
float to = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1]] floatValue];

你有括号在错误的位置。看起来'resultString'行多次出现同样的问题。

于 2012-10-12T18:52:06.113 回答