[英]two pickerview in relation to each other
I am trying to develop a part of my application where i can create two UIPickerView
one depends on the other. 我正在尝试开发应用程序的一部分,在其中我可以创建两个UIPickerView
一个依赖于另一个。 Here is my code below as i have two UIpickerview
(pickerView1 and pickerView2). 这是下面的代码,因为我有两个UIpickerview
(pickerView1和pickerView2)。 when I change the selects of pickerView1 one ,the data must be changed in pickerView2. 当我更改pickerView1的选择时,必须在pickerView2中更改数据。
the problem is when I change the select of pickerView1 every time i have old select. 问题是当我每次有旧选择时都更改pickerView1的选择。 for example if i select the second value of pickerView1 and before i select the first value of pickerView1, then i have on pickerView2 the value of first select and not for second value. 例如,如果我选择pickerView1的第二个值并且在我选择pickerView1的第一个值之前,那么我在pickerView2上具有第一个select的值,而不是第二个值。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrayNo = [[NSMutableArray alloc] init];
[arrayNo addObject:@" 100 "];
[arrayNo addObject:@" 200 "];
arrayNo2= [[NSMutableArray alloc] init];
[arrayNo2 addObject:@" a "];
[arrayNo2 addObject:@" e "];
[arrayNo2 addObject:@" c "];
[arrayNo2 addObject:@" v "];
[arrayNo2 addObject:@" g "];
arrayNo3 = [[NSMutableArray alloc] init];
[arrayNo3 addObject:@""];
NSArray *keys = [NSArray arrayWithObjects:@" ",@"key1", @"key2", nil];
NSArray *objects = [NSArray arrayWithObjects:arrayNo3,arrayNo, arrayNo2, nil];
_dataOfProfile = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
pickerView1.tag = 1;
pickerView2.tag = 2;
[pickerView1 selectRow:0 inComponent:0 animated:NO];
[pickerView2 selectRow:0 inComponent:0 animated:NO];
selectedKey =[keys objectAtIndex:0];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if (pickerView.tag == 1)
{
NSArray *key = [_dataOfProfile allKeys];
return [key count];
}
else
{
if (pickerView.tag == 2)
{
NSArray *keys =[_dataOfProfile objectForKey:selectedKey];
return [keys count];
}
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView.tag == 1)
{
NSArray *keys =[_dataOfProfile allKeys];
return [keys objectAtIndex:row];
}
else
{
if (pickerView.tag == 2)
{
return [[_dataOfProfile objectForKey:selectedKey] objectAtIndex:row];
}
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView.tag == 1)
{
//Means a value just changed on your picker 2!, update datasource for your second picker
[pickerView2 reloadComponent:0];
selectedKey= [[_dataOfProfile allKeys] objectAtIndex:row];
}
}
Try like following - 尝试像以下-
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView.tag == 1)
{
//Means a value just changed on your picker 2!, update datasource for your second picker
//write this line before loading the picker.....
selectedKey= [[_dataOfProfile allKeys] objectAtIndex:row];
[pickerView2 reloadComponent:0];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.