根据uiPickerView以编程方式更改图像

时间:2011-09-19 06:56:46

标签: ios image uipickerview message

我使用的uiPickerView有图像而不是字符串供选择,可以正常显示图像,我从苹果UIcatalog示例中获得了如何放置图像的灵感,

我以编程方式实现它没有IB,

所以问题是,如何根据选定的单元格更改图像?

我有 * MainVC(我的主视图控制器) * CustomView(定义选择器大小) * CustomPickerDataSource(选择器的数据源:)

在CustomPickerDataSource中,发送选择了哪一行的数据

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
NSLog(@"seleccionokkkk: %d", row);
[MainVC entro:row];
}

和我的MainVC

+(void) entro: (int) cuca {

NSLog(@"sumbalo :%d", cuca);
 }

我得到了在我的MainVC上选择的单元格数,这是我想要显示不同图像的地方, 但是在+(void) entro: (int) cuca中选择图像时 我当然会收到警告,因为我用类方法设置实例变量

那么如何根据我收到的细胞编号显示图像?

伪代码: 如果image == 0,则显示image0

在哪里放置我的图像显示代码?,以及如何设置传入消息的变量?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我假设您拥有的图像数量与拾取器中的项目数量相同。按照与选择器视图中相同的顺序将它们放入数组中。然后将要更改的UIImageView更改为所选图片,并将其设置为具有给定索引的数组中的图片。

您可以在entro:方法中执行此操作(这应该是一个实例方法,没有理由不应该或不可以),或者您可以继续在{pickerView:didSelectRow:方法中执行此操作1}}方法。

您可以设置如下图像:

UIImageView *myImageView;
NSArray *myImageArray;
...

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: 
(NSInteger)row inComponent:(NSInteger)component {
    [myImageView setImage:[myImageArray objectAtIndex:row]];
}
相关问题