使用 UIPickerView 在 UIImageView 中选择时显示不同的图像 [英] Using UIPickerView to display different images upon selection in UIImageView

查看:30
本文介绍了使用 UIPickerView 在 UIImageView 中选择时显示不同的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UIPickerView filled with an array of tree types. I also have an empty UIImageView below that I would like to change upon the selection of the picker view. I got a label to change with the selection but cannot get images to change. Here is what I have so far

编辑编辑:现在工作!下面是正确的代码

EDIT EDIT : Now working! Correct Code below

#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

推荐答案

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

What you can do is, have an array filled with the image names (Ex: IMAGES_ARRAY with image names).

-(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]]];
}

这篇关于使用 UIPickerView 在 UIImageView 中选择时显示不同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆