简体   繁体   English

UIPickerView有2个NSMutableArrays?

[英]UIPickerView with 2 NSMutableArrays?

I have a core data based app I am working on, that uses an EditingViewController to control a number of different UI elements that get input from the user which describe attributes of an object that is subsequently stored within my app. 我有一个正在使用的基于核心数据的应用程序,该应用程序使用EditingViewController来控制许多不同的UI元素,这些元素从用户那里获得输入,这些输入描述了随后存储在我的应用程序中的对象的属性。 The EditingViewController.xib file has such elements as a datePicker, textField, numberSlider, and where my problem is coming from a UIPickerView, that are all controlled within one view using .hidden = YES/NO; EditingViewController.xib文件具有诸如datePicker,textField,numberSlider之类的元素,而我的问题来自UIPickerView,这些元素都在一个视图中使用.hidden = YES/NO; expressions. 表达式。 My problem is that I need to populate a UIPickerView in two seperate screens, that need to have two different NSMutableArray sources. 我的问题是,我需要在两个单独的屏幕中填充一个UIPickerView,这些屏幕需要具有两个不同的NSMutableArray源。 Inside my viewDidLoad method I setup my first 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"];

And after that, I populate my UIPickerView *picker with this code: 然后,我用以下代码填充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];
}

And that works fine for the first attribute and array. 对于第一个属性和数组,这很好用。 So then after that, when a different uitableviewcell is selected, the picker is hidden picker.hidden = YES; 因此,在那之后,当选择了另一个uitableviewcell时,选择器将被隐藏picker.hidden = YES; and I need to have another picker, picker2 show up with a different array of information. 我需要另一个选择器,picker2显示不同的信息。 But when I try and duplicate the process, by setting up a whole new picker, calling it picker2, and trying to populate it with a different NSMutableArray that I created right next to the first one (again, all because in my EditingViewController it's all part of the same view, just calling different UI elements depending on the view) I can't get picker2 to populate with the new array. 但是,当我尝试复制该过程时,通过设置一个全新的Picker,将其命名为picker2,并尝试使用在第一个选择器旁边创建的另一个NSMutableArray填充它(再次,这都是因为在我的EditingViewController中,相同的视图,只是根据视图调用不同的UI元素)我无法让picker2填充新数组。 I don't know how to set it up so that my two different arrays can be populated. 我不知道如何设置它,以便可以填充两个不同的数组。 Do I need two picker views? 我需要两个选择器视图吗? Can it be done with just one? 可以只用一个完成吗? What is the proper syntax in the - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component; - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;的正确语法是什么- (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component; method to make the two arrays viewable seperately? 使两个数组分别可见的方法? I hope someone can help me out on this, thank you in advance! 希望有人可以帮助我,在此先谢谢您!

You are asking to use the same object as the delegate for two pickers. 您要为两个选择器使用与委托相同的对象。

In each of the delegate methods, the picker is provided as the first argument for this purpose. 为此,在每个委托方法中,选择器均作为第一个参数提供。 You can check which picker is passed in and return the appropriate data for that picker. 您可以检查传入的选择器并返回该选择器的适当数据。

For example, if you add a property for the first picker named "pickerOne" and you second mutable array is called "arrayTwo", the delegate methods would look like this: 例如,如果为第一个选择器添加一个名为“ 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];
    }
}

Also, you can populate your array like this (if you just have a static list of strings, the array does not need to be mutable): 另外,您可以像这样填充数组(如果您只有一个静态字符串列表,则该数组不需要是可变的):

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