3单视图控制器中的UIPicker视图

时间:2013-12-31 10:13:17

标签: iphone objective-c

我的视图控制器中有3个选择器视图 picker1包含2行Audio和Video.picker2包含产品类型,如果在picker1中选择了audio,则picker2应填充(transistor,walkman,ipod),如果在picker1中选择了video,则picker2应填充({{1}如果在picker2中选择了walkman,那么picker3应填充(tv,screen,monitor)。 我对if条件感到困惑....

sony,philips,jbl

2 个答案:

答案 0 :(得分:1)

你必须有条件地加载picker2和picker 3,如下所示:

picker1Array=[[NSMutableArray alloc] initWithObjects:@"Audio",@"Video", nil]
if ([[picker1array objectAtIndex:[picker1 selectedRowInComponent:0]] isEqual:@"Audio"])
{
    picker2Array=[[NSMutableArray alloc] initWithObjects:@"transistor",@"walkman",@"ipod", nil];
}
else {
    picker2Array=[[NSMutableArray alloc] initWithObjects:@"tv",@"screen",@"monitor", nil];
}
 [picker2 reloadAllComponents];  

if ([[picker2array objectAtIndex:[picker2 selectedRowInComponent:0]] isEqual:@"walkman"]){
   picker3Array=[[NSMutableArray alloc] initWithObjects:@"sony",@"philips",@"jbl", nil];
} 
[picker3 reloadAllComponents];  

答案 1 :(得分:0)

请尝试以下代码。

<。>在.h文件中请声明

NSMutableArray *randCatSel;
    NSMutableArray *randSubCatSel;
    NSMutableArray *randItemSel;
    UIPickerView *packerView;

并在.m文件中编写此代码。

- (void)fetchedData{

    randCatSel =[[NSMutableArray alloc] initWithObjects:@"Audio",@"Video", nil];

    packerView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
    packerView.delegate=self;
    packerView.dataSource=self;
    packerView.tag=1;
    [self.view addSubview:packerView];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
    if(pickerView.tag==1)
    {
        return [randCatSel count];
    }
    else if(pickerView.tag==2)
    {
        return [randSubCatSel count];
    }
    else if(pickerView.tag==3)
    {
        return [randItemSel count];
    }
    else
    {
        return 0;
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row   forComponent:(NSInteger)component
{
    if(pickerView.tag==1)
    {
        return [randCatSel objectAtIndex:row];
    }
    else if(pickerView.tag==2)
    {
        return [randSubCatSel objectAtIndex:row];
    }
    else if(pickerView.tag==3)
    {
        return [randItemSel objectAtIndex:row];
    }
    else
    {
        return 0;
    }



}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(pickerView.tag==1)
    {
        if(randSubCatSel.count>0)
        {
            randSubCatSel=nil;
        }

        if([[randCatSel objectAtIndex:row] isEqualToString:@"Audio"])
        {
                    randSubCatSel=[[NSMutableArray alloc] initWithObjects:@"Transistor",@"walkman",@"ipod", nil];
        }
        else
        {
                    randSubCatSel=[[NSMutableArray alloc] initWithObjects:@"TV",@"Screen",@"Monitor", nil];
        }

        [packerView removeFromSuperview];
        packerView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
        packerView.delegate=self;
        packerView.dataSource=self;
        packerView.tag=2;
        [self.view addSubview:packerView];
    }
    else if(pickerView.tag==2)
    {
        //do coding according to randSubCatSel for randItemSel
    }
}

请注意,这是您的要求的示例代码。你需要做出相应的改变。 我希望这段代码可以帮到你。