繁体   English   中英

UIPickerView有2个NSMutableArrays?

[英]UIPickerView with 2 NSMutableArrays?

我有一个正在使用的基于核心数据的应用程序,该应用程序使用EditingViewController来控制许多不同的UI元素,这些元素从用户那里获得输入,这些输入描述了随后存储在我的应用程序中的对象的属性。 EditingViewController.xib文件具有诸如datePicker,textField,numberSlider之类的元素,而我的问题来自UIPickerView,这些元素都在一个视图中使用.hidden = YES/NO; 表达式。 我的问题是,我需要在两个单独的屏幕中填充一个UIPickerView,这些屏幕需要具有两个不同的NSMutableArray源。 在我的viewDidLoad方法中,我设置了第一个NSMutableArray:

listOfItems = [[NSMutableArray alloc] init];
[listOfItems addObject:@"Greenland"];
[listOfItems addObject:@"Switzerland"];
[listOfItems addObject:@"Norway"];
[listOfItems addObject:@"New Zealand"];
[listOfItems addObject:@"Greece"];
[listOfItems addObject:@"Rome"];
[listOfItems addObject:@"Ireland"];

然后,我用以下代码填充UIPickerView * picker:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
{
    return 1;
}

- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
    label.text= [listOfItems objectAtIndex:row];// The label is simply setup to show where the picker selector is at
}

- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;
{
    return 8;//[listOfItems count];

}

- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;
{
    return [listOfItems objectAtIndex:row];
}

对于第一个属性和数组,这很好用。 因此,在那之后,当选择了另一个uitableviewcell时,选择器将被隐藏picker.hidden = YES; 我需要另一个选择器,picker2显示不同的信息。 但是,当我尝试复制该过程时,通过设置一个全新的Picker,将其命名为picker2,并尝试使用在第一个选择器旁边创建的另一个NSMutableArray填充它(再次,这都是因为在我的EditingViewController中,相同的视图,只是根据视图调用不同的UI元素)我无法让picker2填充新数组。 我不知道如何设置它,以便可以填充两个不同的数组。 我需要两个选择器视图吗? 可以只用一个完成吗? - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;的正确语法是什么- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component; 使两个数组分别可见的方法? 希望有人可以帮助我,在此先谢谢您!

您要为两个选择器使用与委托相同的对象。

为此,在每个委托方法中,选择器均作为第一个参数提供。 您可以检查传入的选择器并返回该选择器的适当数据。

例如,如果为第一个选择器添加一个名为“ pickerOne”的属性,而第二个可变数组称为“ arrayTwo”,则委托方法将如下所示:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)picker;
{
    return 1; // assuming both pickers only have 1 component
}

- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
    // The label is simply setup to show where the picker selector is at
    if (picker == self.pickerOne) {
        label.text= [listOfItems objectAtIndex:row];
    } else {
        label.text= [arrayTwo objectAtIndex:row];
    }
}

- (NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component;
{
    if (picker == self.pickerOne) {
        return [listOfItems count];
    } else {
        return [arrayTwo count];
    }
}

- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;
{
    if (picker == self.pickerOne) {
       return [listOfItems objectAtIndex:row];
    } else {
        return [arrayTwo objectAtIndex:row];
    }
}

另外,您可以像这样填充数组(如果您只有一个静态字符串列表,则该数组不需要是可变的):

listOfItems = [[NSArray alloc] initWithObjects:@"Baptisms",@"Greenland",@"Switzerland",@"Norway",@"New Zealand",@"Greece",@"Rome",@"Ireland",nil];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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