Search code examples
iosuipickerview

Use a UIPickerView in a Navigation Controller


UPDATE! The error was in the delegate and the dataSource of the picker view :)

Ok, here is the problem: I want to implement a UIPickerView in the a view of a class "infoGeneral" that I already created.

The hierarchy of the app so far is like this: When you click the "Crear proyecto nuevo" button, you go to the other view, where the UiPickerView is located.

App hierarchy (Dropbox, I can't post images)

I saw a tutorial of how to use a UiPicker view, but in a default view. So I followed the tutorial, but instead of use some inits in viewDidLoad, I did it on awakeFromNib, (because viewDidLoad is an ViewController method,not a View one)

I setted the dataSource and delegate of the PickerView, to the View where it is.

But I get this error:

2014-12-07 19:11:57.174 calcMuroLosas[6272:182705] -[UIViewController numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7fb620c88300

And I know the error is because I'm using a Navigation controller.

So can somebody tell me whan can I do to use the pickerview in that hierarchy?

Here are my files:

infoGeneral.h

#import <UIKit/UIKit.h>

@interface infoGeneral : UIView <UIPickerViewDelegate, UIPickerViewDataSource>

@property (retain, nonatomic) IBOutlet UIPickerView *picker;
- (IBAction)buttonPressed:(id)sender;

@end

infoGeneral.m

#import "infoGeneral.h"
@interface infoGeneral ()
@property (strong, nonatomic) NSArray *arrayTipoCodigoEsfuerzosAdmisibles;
@end
@implementation infoGeneral

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib
{

    NSArray *dataTipoCodigoEsfuerzosAdmisibles = [[NSArray alloc] initWithObjects:@"MSJC", @"Guatemala", @"El Salvador", @"Honduras", @"Nicaragua", @"Costa Rica", @"Panamá (MSJC)", @"México", @"Chile", @"Colombia", @"Canada", @"Perú", @"Dominicada (similar a MSJC)", @"Ecuador", @"Otro", nil];
    self.arrayTipoCodigoEsfuerzosAdmisibles = dataTipoCodigoEsfuerzosAdmisibles;
}

- (IBAction)buttonPressed:(id)sender {
    NSString *select = [_arrayTipoCodigoEsfuerzosAdmisibles objectAtIndex:[_picker selectedRowInComponent:0]];
    NSString *title = [[NSString alloc]initWithFormat:@"You selected %@!", select];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"YAY!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];

}

#pragma mark Picker Data Sources Methods
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [_arrayTipoCodigoEsfuerzosAdmisibles count];
}
#pragma mark Picker Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [_arrayTipoCodigoEsfuerzosAdmisibles objectAtIndex:row];
}
@end

Solution

  • easiest probably is to add following to your infoGeneral viewDidLoad: method

    [_picker setDelegate:self];
    

    best probably to set delegate in your storyboard via your picker view attributes inspector pane.