UIPickerView有两行

时间:2012-10-12 18:27:20

标签: ios xcode uipickerview

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

提前致谢:)

2 个答案:

答案 0 :(得分: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为)清楚 {

}

答案 1 :(得分: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'行有多次出现同样的问题。