问题
So ive got my UI picker but it is displaying the same data for each of the UI pickers but i would like it to display the moustache array in one ui picker and the colour one in the other. Currently shown in the image it is assigning the same data to each array.
- (void)viewDidLoad
{
[super viewDidLoad];
_colourSourceArray = [[NSArray alloc] initWithObjects:@"No Frame",@"Red", @"Green", @"Blue", @"Black",@"Yellow", nil ];
_MustacheArray = [[NSArray alloc]initWithObjects:@"None",@"Pencil",@"The Professor",@"The Regent",@"Hipster",@"Super Mario", nil];
[_picker selectRow:0 inComponent:0 animated:YES];
[_notcolourpicker selectRow:1 inComponent:0 animated:YES];
_picker.tag=0;
_notcolourpicker.tag=1;
}
- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch (component) {
case 0:
return _colourSourceArray.count;
break;
case 1:
return _MustacheArray.count;
default:
break;
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component) {
case 0:
return [_colourSourceArray objectAtIndex:row];
break;
case 1:
return [_MustacheArray objectAtIndex:row];
default:
break;
}
return 0;
}
-(IBAction)returnToExportSettingsVC:(UIStoryboardSegue *)segue
{
// Nothing needed here.
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {
NSLog(@"First");
NSString *s = _colourSourceArray[row];
_selectedcolour = s;
NSLog(_selectedcolour);
}
else
if(component == 1){
NSLog(@"Second");
NSString *d = _MustacheArray[row];
_selectedmustache=d;
NSLog(_selectedmustache);
}
/// Used if you wist to assign the selected row to a label to show a users selection.
//_label.text= [_MustacheArray objectAtIndex:[mostachepicker selectedRowInComponent:1]];
}
回答1:
The problem is that both picker views are calling into the same data source / delegate methods. If you're doing to structure your code that way, you will need to examine the pickerView
parameter to see which picker view this is, and switch on that.
来源:https://stackoverflow.com/questions/38414185/two-ui-pickers-in-the-same-view-controller-objective-c