0
votes

I am trying the basic UI picker view with the following code,

- (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

Getting an exception when i run stating

2013-12-09 15:46:11.174 pickerExample[20718:a0b] -[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x9880e80 2013-12-09 15:46:11.260 pickerExample[20718:a0b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x9880e80'.

I have linked the datasource and delegates to the current view controller. Can anyone guide where i am going wrong!!

1
In viewDidLoad at which line your app get crashed ? - Maulik
I dont think its crashing in viewdidload, its telling in UIView numberOfComponentsInPickerView - bharath
Yup, it passes through the viewdidload perfectly and crashes after that!! - bharath

1 Answers

0
votes

Your problem is call of [self.MainPicker selectRow:1 inComponent:0 animated:NO]; method before setting datasource. actually you are trying to select a row before even setting up UIPicker so set your _countryNames and _exchangeRates before calling this method as below...

in your viewDidLoad

Initialization of datasource

_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];  

// method call

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