使用UIPickerView来显示的UIImageView选择时不同的图像(Using UIPick

2019-10-19 13:26发布

我充满了树类型的数组一个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

Answer 1:

你可以做的是,有一个数组填满图象名称(例如: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]]];
}


文章来源: Using UIPickerView to display different images upon selection in UIImageView