I am writing code to programmatically create a view. In this view I am creating two UIPickerViews, however, as the title explains, when I go to populate the lists with data, all of the data goes into the second table, and none in the first. I can see where exactly the problem is, but don’t know how to fix it. Here is the code with editing around non-related parts.
in the below section is where i create the pickerview. Essentially I have a large list of data. If i see any occurance of “RadioButton” I create a PickerView. RadioButton can appear more than once and is where my issue lies.
for(int i = 0; i < [list count]; i++){
...
else if([input.controlTypeName compare:@"RadioButton"] == NSOrderedSame){
[radioList addObject:input.sourceText];
radioPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(50, y, 220, 200)];
radioPicker.delegate = self;
radioPicker.showsSelectionIndicator = YES;
[inputsView addSubview:radioPicker];
y = y+220;
}
the delegate methods are as follows..
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if([pickerView isEqual: radioPicker]){
return [radioList count];
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if([pickerView isEqual: radioPicker]){
id myArrayElement = [radioList objectAtIndex:row];
NSString *title = myArrayElement;
NSLog(@"title to fill radio:%@", title);
return title;
}
}
// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
int sectionWidth = 200;
return sectionWidth;
}
Essentially what seems to be happening is on the 2nd creation of the ‘RadioButton’ ListPicker, the first creation loses the tag ‘radioPicker’ and the line
if([pickerView isEqual: radioPicker]){
fails. So all of the data is pushed into the second(and latest) creation. But I don’t know how to have this not happen. Any ideas would be great.
Thanks.
The issue is that you have a single instance variable,
radioPicker. It’s fine to have twoUIPickerViews and be the delegate of both. However, you need to base the delegate methods on the passed-in value, not the (single) instance variable.There’s actually no need to have
radioPicker. Once you sendaddSubview:, just autorelease the view (unless you’re using ARC).In order to distinguish the views, you might want to set the
tagproperty, and use that in things like thetitleForRow:delegate method.