Question

I am trying to create a PickerView view programatically.

I have followed all of the necessary steps however the picker just simply appears and then disappears straight away. No data can be seen.

I am setting the delegate and data source and I have implemented the necessary methods. Here is my code:

in .h

@interface NJATimeEntryViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *customerPicker;
@property (nonatomic, strong) NSArray *pickerTitles;

@end

in .m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.pickerTitles = @[@"ONE", @"TWO", @"THREE"];

    self.customerPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
    self.customerPicker.dataSource = self;
    self.customerPicker.delegate = self;

    [self.view addSubview:self.customerPicker];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

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


-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [[self pickerTitles] objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"Selected Row %d", row);
}

All help is appreciated thanks.

FIXED:

All i did was set the background color of the view controllers view and now it works. I see this as a bug as no where is it stated that the background color has to be set.

Was it helpful?

Solution

Set Some Background color ( except black )of your view, then try,

self.view.backgroundColor=[UIColor yellowColor];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top