UIPickerView 带有 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 显示不同的信息数组。但是,当我尝试复制该过程时,通过设置一个全新的选择器,将其称为 picker2,并尝试用我在第一个选择器旁边创建的不同 NSMutableArray 填充它(同样,所有这些都是因为在我的 EditingViewController 中,它都是相同的视图,只是根据视图调用不同的 UI 元素)我无法让 picker2 填充新数组。我不知道如何设置它以便可以填充我的两个不同的数组。我需要两个选择器视图吗?一个人就能搞定吗? - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent: (NSInteger)component;
方法中使两个数组可单独查看的正确语法是什么?我希望有人能帮我解决这个问题,先谢谢您了!
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. 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;
expressions. My problem is that I need to populate a UIPickerView in two seperate screens, that need to have two different NSMutableArray sources. Inside my viewDidLoad method I setup my first 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:
- (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;
and I need to have another picker, picker2 show up with a different array of information. 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. 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;
method to make the two arrays viewable seperately? I hope someone can help me out on this, thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要求使用相同的对象作为两个选择器的委托。
为此,在每个委托方法中,选择器都作为第一个参数提供。您可以检查传入的选择器并返回该选择器的适当数据。
例如,如果您为第一个选择器添加一个名为“pickerOne”的属性,而第二个可变数组称为“arrayTwo”,则委托方法将如下所示:
另外,您可以像这样填充数组(如果您只有一个字符串的静态列表,数组不需要是可变的):
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:
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):