【发布时间】:2016-07-16 18:17:20
【问题描述】:
所以我得到了我的 UI 选择器,但它为每个 UI 选择器显示相同的数据,但我希望它在一个 ui 选择器中显示小胡子数组,在另一个中显示颜色。当前显示在图像中,它正在为每个数组分配相同的数据。
- (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]];
}
【问题讨论】:
-
只需将
tag添加到故事板中的选择器视图中,然后在从数据源返回数据时,通过执行if picker.tag == 1 { ... } else if picker.tag == 2 { ... }来检查哪个选择器正在请求数据