我试图让2个采摘者进入一个视图控制器,我真的很挣扎。我创建了两个数组,我认为我已经将它们连接起来了,但它们同时出现了拾取器和从同一数据源获取数据的情况。第一个选择器应该更改它所选择的数据(myLabel.text),但它也会改变所选择的职业,就像第二个选择器那样。如果我能在这件事上得到一些帮助,我将不胜感激。谢谢
我也尝试过搜索其他人面临的类似问题,但出于某种原因我无法将其链接到我的代码。
- (void)viewDidLoad {
[super viewDidLoad];
myLabel.text = @"Use the scroller above...";
datePickerView.delegate = self;
CareerPickerView.dataSource = self;
_pickerViewArray = @[@"Before September 2012",@"After September 2012"];
CareerViewArray = @[@"This is choice 1",@"This is choice 2"];
CareerPickerView.delegate = self;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *dateSelected = [_pickerViewArray objectAtIndex:row];
NSString *careerSelected = [CareerViewArray objectAtIndex:row];
myLabel.text = dateSelected;
startingsalary.text = careerSelected;
if ([dateSelected isEqualToString:@"Before September 2012"])
myLabel.text = @"Before September 2012";
if ([dateSelected isEqualToString:@"After September 2012"])
myLabel.text = @"After September 2012";
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return _pickerViewArray.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return _pickerViewArray[row];
}
@end
答案 0 :(得分:1)
您必须在方法delegate
pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
检查哪个UIPickerView
正在调用该方法并执行您需要的操作
答案 1 :(得分:1)
您应该维护正确选择的数组和选择器视图。
请按照以下代码:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(pickerView == datePickerView)
{
NSString *dateSelected = [_pickerViewArray objectAtIndex:row];
myLabel.text = dateSelected;
}
else if(pickerView == CareerPickerView)
{
NSString *careerSelected = [CareerViewArray objectAtIndex:row];
startingsalary.text = careerSelected;
}
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if(pickerView == datePickerView)
{
return _pickerViewArray.count;
}
else if(pickerView == CareerPickerView)
{
return CareerViewArray.count;
}
return 1;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if(pickerView == datePickerView)
{
return _pickerViewArray[row];
}
else if(pickerView == CareerPickerView)
{
return CareerViewArray[row];
}
return nil;
}
答案 2 :(得分:0)
将标签应用于您的UIPickerview,并在每个委托方法中将比较选择器与标签进行比较,确保您在大括号内返回对象以区分标签