2012-10-12 95 views
0

我想爲某些單位的東西做一個簡單的轉換器,我似乎無法管理從兩列中獲取數據,並將它們變成我可以計算的變量。我也收到一些錯誤,如果有人願意,我會很感激。錯誤出現在#pragma標記 - PickerView委託中,我認爲我的代碼中有90%是錯誤的。UIPickerView與兩行

這裏是完整的.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 

感謝提前:)

+0

有什麼錯誤?不要讓我們猜測。 :)他們是編譯錯誤還是運行時錯誤?清楚地表明確切的錯誤以及它與哪一行相關聯。 – rmaddy

+0

對不起,編譯器錯誤。幾乎每一個生活在pragma pickerview委託之外。他們大多數是「失蹤」;「」,編譯錯誤。 –

回答

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爲)明確 {

}

+0

這不是關於計算,它要做的東西工作,並從wheely檢索數字。 –

+0

謝謝,這幫助了很多。我仍然得到「預期」;'「在 - float from = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0] floatValue]]; –

+0

好友你使用了錯誤的大括號:像上面提到的那樣使用它: float from = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue]; – Bhupendra

0

這兩條線:

應該是:

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

你在錯誤的地方支架。它看起來像'resultString'行有多次相同的問題。

+0

非常感謝。當編譯該行時,我仍然會得到「預期」;「」。有小費嗎? –

+0

哦,等一下。我的錯。我更新了代碼。 – rmaddy

+0

非常感謝! :) –