How to set data for UIPickerView?

I am trying to add PickerView - But I see a black screen ... What am I doing wrong ???

UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];

    [self.view addSubview:myPickerView];

      

+3


source to share


1 answer


Implement the UIPickerViewDataSource methods :

numberOfComponentsInPickerView:pickerView:numberOfRowsInComponent:

      

and UIPickerViewDelegate :

pickerView:titleForRow:forComponent:

      

Don't forget to set properties:



UIPickerView * myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
myPickerView.delegate = self;
myPickerView.dataSource = self;
[self.view addSubview:myPickerView];

      

And in the .h file, don't forget to put:

<UIPickerViewDelegate, UIPickerViewDataSource>

      

I usually use NSMutableArray

filled with strings to fill in the data. Hope it helps!

+5


source







All Articles