简体   繁体   中英

show image in different image view when button clicked.

I've been trying to make this code work but can't really get final part together.

I got 2 rows of 6 image views,

Imageview 1 to 6 and 7 to 12.

First I click 6 buttons out of my 26 buttons (AZ) and they display in the first 6 views with this code:

- (void)setImage:(UIImage *)image {
if (imageview1.image == nil) {
    [imageview1 setImage:image];
} else if (imageview2.image == nil){
    [imageview2 setImage:image];
} else if (imageview3.image == nil){
    [imageview3 setImage:image];
} else if (imageview4.image == nil){
    [imageview4 setImage:image];
} else if (imageview5.image == nil){
    [imageview5 setImage:image];
} else if (imageview6.image == nil){
    [imageview6 setImage:image];
}
 }

-(IBAction)showA {

UIImage *img = [UIImage imageNamed:@"A.png"];

[self setImage:img];

}

now when I click the button play I want the images to change from view 1 - 6 to 7 - 12.

I hope there is someone who can explain me how to do this, I tried a couple of ways now but Can't really get it working, maybe making variable strings then get the image from that?

hope someone can explain me or has some sample code for me thanks

Like this?

[imageview7 setImage:imageView1.image];
[imageview8 setImage:imageView2.image];
etc...
[imageview1 setImage:nil];
[imageview2 setImage:nil];
etc...

It is not because you did not set up an image for your UIImageView that it did not alloc a space for the image.

In the other other way CGImage represents the "data of your image". If you did not set up the image but UIImageView alloc the UIImage for you, you are sure that the "data of this image" will be nil (because there is no image).

So you should try that out:

if([imageview1.image CGImage]==nil){[imageview1 setImage:image];}

Hope that help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM