以编程方式创建两个UIPickerView,但所有值都转到第二个



我正在编写代码以编程方式创建视图。在这个视图中,我创建了两个UIPickerViews,但是,正如标题所解释的,当我用数据填充列表时,所有数据都会进入第二个表,而第一个表中没有数据。我能看到问题的确切位置,但不知道如何解决。以下是围绕非相关部分进行编辑的代码。

在下面的部分中,我创建了pickerview。从本质上讲,我有一大堆数据。如果我看到任何"RadioButton"的出现,我会创建一个PickerView。RadioButton可以出现不止一次,这就是我的问题所在。

 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;
    }

委托方法如下。。

       - (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;
 }

本质上,似乎正在发生的是在"RadioButton"ListPicker的第二次创建时,第一次创建丢失了标记"radioPicker"和行

if([pickerView isEqual: radioPicker]){ 

失败。因此,所有数据都被推送到第二个(也是最新的)创建中。但我不知道怎么才能不发生这种事。任何想法都很棒。

谢谢。

问题是您只有一个实例变量radioPicker。有两个UIPickerView并同时作为这两个的代表是可以的。但是,您需要将委托方法基于传入的值,而不是(单个)实例变量。

实际上不需要radioPicker。发送addSubview:后,只需自动释放视图(除非使用ARC)。

为了区分视图,您可能需要设置tag属性,并在titleForRow:委托方法中使用该属性。

最新更新