gpt4 book ai didi

iphone - 以编程方式创建两个 UIPickerViews 但所有值都排在第二位

转载 作者:行者123 更新时间:2023-11-28 23:07:32 25 4
gpt4 key购买 nike

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

在下面的部分是我创建 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;

}

委托(delegate)方法如下..

       - (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 并成为两者的委托(delegate)是很好的。但是,您需要将委托(delegate)方法基于传入的值,而不是(单个)实例变量。

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

为了区分 View ,您可能需要设置 tag 属性,并在 titleForRow: 委托(delegate)方法中使用它。

关于iphone - 以编程方式创建两个 UIPickerViews 但所有值都排在第二位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9068023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com