简体   繁体   中英

how to set data for UIPickerView?

I am tryign to add a 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];

Implement the UIPickerViewDataSource methods:

– numberOfComponentsInPickerView:
– pickerView:numberOfRowsInComponent:

and the UIPickerViewDelegate method:

– pickerView:titleForRow:forComponent:

Don't forget to set the 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, remember to put:

<UIPickerViewDelegate, UIPickerViewDataSource>

I usually use an NSMutableArray filled with strings in order to fill in the data. Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM