在UIImageView中选择时使用UIPickerView显示不同的图像

时间:2014-03-10 19:53:43

标签: ios uiimageview uipickerview

我有一个填充了树类型数组的UIPickerView。我下面还有一个空的UIImageView,我想在选择器选择视图时改变它。我有一个标签随着选择而改变,但无法改变图像。这是我到目前为止所拥有的

修改 编辑:现在正在工作!正确的代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    dataArray = [[NSArray alloc]initWithObjects:@"Birch", @"Elm", @"Maple", @"Oak", @"White Pine",@"Willow", nil];
    myPicker.delegate = self;
    imageArray = [[NSArray alloc]initWithObjects:@"birch_river.png",@"elm_american.png",@"maple_bigtooth.png", @"oak_bur.png", @"white_pine.png", @"oak_willow.png", nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [dataArray count];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    myLabel.text = [dataArray objectAtIndex:row];
    myImage.image = [UIImage imageNamed:[imageArray objectAtIndex:row]];
}

 -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:  (NSInteger)component
{
    return [dataArray objectAtIndex:row];
}


@end

1 个答案:

答案 0 :(得分:0)

你可以做的是,有一个数组填充图像名称(例如:带图像名称的IMAGES_ARRAY)。

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:   (NSInteger)component
{
    myLabel.text = [dataArray objectAtIndex:row];
    YOUR_IMG_VIEW = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[IMAGES_ARRAY ObjectAtIndex.row]]];
}