开发者

change image programatically according to uiPickerView

开发者 https://www.devze.com 2023-04-05 14:42 出处:网络
Im using a uiPickerView that haves images instead of strings for the selections, is working ok for showing the images, I got inspiration from the apple UIcatalog example on how to put the images,

Im using a uiPickerView that haves images instead of strings for the selections, is working ok for showing the images, I got inspiration from the apple UIcatalog example on how to put the images,

Im implementing it programatically no IB,

So the question is, how to change an image according to the selected cell?

i have * MainVC (my main view controller) * CustomView (defining picker size) * CustomPickerDataSource (the data source for the picker :)

in CustomPickerData开发者_C百科Source, send the data for wich row was selected

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

and in my MainVC

+(void) entro: (int) cuca {

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

I get the number of cell selected on my MainVC, which is where I want to show the different images, but when selecting the image in my +(void) entro: (int) cuca I get warnings of course as Im setting instance variables with a class method,

so how can I show the image according to which cell number I receive?

Pseudocode: if image == 0, show image0

Where to put my image showing code?, and how to set the variable for the incoming message?

thanks a lot!


I'd assume you have as many images as you have items in the picker. Put them into an array in the same order as you did in the picker view. Then take the UIImageView that you want to change to the selected picture, and set it to the picture in the array with the given index.

You can do this in your entro: method (which should be an instance method, no reason that it shouldn't or can't be), or you can just go ahead and do it in the pickerView:didSelectRow: method.

You can set images like this:

UIImageView *myImageView;
NSArray *myImageArray;
...

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

精彩评论

暂无评论...
验证码 换一张
取 消