两个pickerview正在改变一个imageview

时间:2013-10-14 12:32:05

标签: ios uipickerview

我正在开发我的第一个iOS应用程序并且卡住了,我搜索了很多,但我找不到任何解决方案。

我有一个带有两个组件的UIPickerView(Picker1有5个数组对象,Picker2有2个)和UIImageView,我需要根据在两个选择器上选择的数据设置不同的图像。

前:

    Picker1 - A and Picker2 - A | Image - AA
    Picker1 - B and Picker2 - A | Image - BA
    Picker1 - A and Picker2 - B | Image - AB
    Picker1 - B and Picker2 - B | Image - BB
    ...

这是我的代码:

#define MOVEIS 0
#define CORES 1

- (void)viewDidLoad
{
    [super viewDidLoad];

    ContainerPickerView.frame = CGRectMake(0, 390, 320, 207);
    ContainerPickerView.backgroundColor = [UIColor whiteColor];

    CatalogoPicker.showsSelectionIndicator = TRUE;

    self.CoresMoveis=[[NSArray alloc] initWithObjects:@"Preto",@"Vermelho",@"Castanho",@"Cinzento",@"Branco", nil];

    self.CoresChao=[[NSArray alloc] initWithObjects:@"Escuro", @"Claro", nil];

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == MOVEIS) {
        return [CoresMoveis count];
    }
    if (component == CORES) {
        return [CoresChao count];
    }
    return 0;
}

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    if (component == MOVEIS) {
        return [CoresMoveis objectAtIndex:row];
    }
    if (component == CORES) {
        return [CoresChao objectAtIndex:row];
    }
    return 0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Preto"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_preto_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Vermelho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_vermelho_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Castanho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_castanho_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Cinzento"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_cinzento_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Branco"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_branco_chao_escuro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Vermelho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Claro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_vermelho_chao_claro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Castanho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Claro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_castanho_chao_claro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Cinzento"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Claro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_cinzento_chao_claro.jpg"];

    }
    else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqual: @"Branco"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqual: @"Claro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_branco_chao_claro.jpg"];
    }
}

1 个答案:

答案 0 :(得分:1)

而不是

isEqual:@"Preto"

使用:

isEqualToString:@"Preto"

所以它会是这样的:

if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Preto"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Escuro"]) {

        _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_preto_chao_escuro.jpg"];

    }
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Vermelho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Escuro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_vermelho_chao_escuro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Castanho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Escuro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_castanho_chao_escuro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Cinzento"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isisEqualToString: @"Escuro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_cinzento_chao_escuro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Branco"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Escuro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_branco_chao_escuro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Vermelho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Claro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_vermelho_chao_claro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Castanho"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Claro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_castanho_chao_claro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Cinzento"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Claro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_cinzento_chao_claro.jpg"];

}
else if ([[CoresMoveis objectAtIndex:[pickerView selectedRowInComponent:0]] isEqualToString: @"Branco"] || [[CoresChao objectAtIndex:[pickerView selectedRowInComponent:1]] isEqualToString: @"Claro"]) {

    _ImageView_Catalogo.image = [UIImage imageNamed:@"movel_branco_chao_claro.jpg"];
}

希望这有效!祝你好运!