2013-12-09 54 views
0

我想用下面的代碼基本UI選擇器視圖,UIPicker視圖IOS7例外

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.MainPicker.delegate = self; 
    self.MainPicker.dataSource = self; 
    [self.MainPicker selectRow:1 inComponent:0 animated:NO]; 
    _countryNames = @[@"Australia (AUD)", @"China (CNY)", 
         @"France (EUR)", @"Great Britain (GBP)", @"Japan (JPY)"]; 
    //NSLog(@"%@", [_countryNames objectAtIndex:0]); 

    _exchangeRates = @[ @0.9922f, @6.5938f, @0.7270f, 
         @0.6206f, @81.57f]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
     inComponent:(NSInteger)component 
{ 
    float rate = [_exchangeRates[row] floatValue]; 
    float dollars = 10.0; 
    float result = dollars * rate; 

    NSString *resultString = [[NSString alloc] initWithFormat: 
           @"%.2f USD = %.2f %@", dollars, result, 
           _countryNames[row]]; 
    NSLog(@"%@",resultString); 
    [pickerView reloadAllComponents]; 
} 
- (IBAction)Submit:(id)sender { 
} 
-(IBAction)textFieldReturn:(id)sender 
{ 
    [sender resignFirstResponder]; 
} 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView 
numberOfRowsInComponent:(NSInteger)component 
{ 
    return _countryNames.count; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView 
      titleForRow:(NSInteger)row 
      forComponent:(NSInteger)component 
{ 
    return _countryNames[row]; 
} 
@end 

獲得一個例外,當我運行,說明

2013年12月9日15:46:11.174 pickerExample [20718:A0B] - [UIView的 numberOfComponentsInPickerView:]:無法識別的選擇發送到 實例0x9880e80 2013年12月9日15:46:11.260 pickerExample [20718:A0B] *終止應用程序由於未捕獲的異常「NSInval idArgumentException」,原因: ' - [UIView的 numberOfComponentsInPickerView:]:無法識別的選擇發送到 實例0x9880e80'。

我已經將數據源和委託鏈接到當前視圖控制器。任何人都可以指導我哪裏錯了!

+0

在'viewDidLoad'你的應用崩潰的哪一行? – Maulik

+0

我不認爲它轟然viewDidLoad中,其在UIView的numberOfComponentsInPickerView – bharath

+0

燁講,它通過viewDidLoad中完美和崩潰後! – bharath

回答

0

你的問題是設置datasource[self.MainPicker selectRow:1 inComponent:0 animated:NO];方法的調用。其實你想在你的viewDidLoad中選擇行之前甚至setting up UIPicker 所以調用下面這個方法之前設置你的_countryNames_exchangeRates ...

數據源的初始化

_countryNames = @[@"Australia (AUD)", @"China (CNY)", 
         @"France (EUR)", @"Great Britain (GBP)", @"Japan (JPY)"]; 
    //NSLog(@"%@", [_countryNames objectAtIndex:0]); 

    _exchangeRates = @[ @0.9922f, @6.5938f, @0.7270f, 
         @0.6206f, @81.57f]; 

//方法調用

self.MainPicker.delegate = self; 
     self.MainPicker.dataSource = self; 
     [self.MainPicker selectRow:1 inComponent:0 animated:NO];