简体   繁体   English

单击按钮时以不同的图像视图显示图像。

[英]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, 我有2行包含6张图片,

Imageview 1 to 6 and 7 to 12. Imageview 1至6和7至12。

First I click 6 buttons out of my 26 buttons (AZ) and they display in the first 6 views with this code: 首先,在我的26个按钮(AZ)中单击6个按钮,它们在前6个视图中显示如下代码:

- (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. 现在,当我单击按钮播放时,我希望图像从视图1-6更改为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. 不是因为您没有为UIImageView设置图像,而是因为它没有为图像分配空间。

In the other other way CGImage represents the "data of your image". 用另一种方式CGImage表示“图像数据”。 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). 如果您没有设置图像,而是UIImageView为您分配了UIImage,那么您可以确保“此图像的数据”为nil(因为没有图像)。

So you should try that out: 因此,您应该尝试一下:

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

Hope that help you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在FirstViewController中单击按钮时在SecondViewController中显示图像 - Show image in SecondViewController when button is clicked in FirstViewController 单击其他按钮时更改UIButton的图像 - Change the image of a UIButton when a different Button is clicked 单击按钮时解析下载的PDF。 - Parse downloading PDF when button is clicked. 在FirstViewController中单击按钮时如何在SecondViewController中显示图像 - How to show image in SecondViewController when button is clicked in FirstViewController 单击按钮时显示图像 - Displaying image when button clicked 单击另一个按钮时更改按钮图像 - Changing button image when another button is clicked 单击按钮后如何将按钮图像传递到图像视图? - how do I pass my button image to an Image view when its clicked? 单击按钮时如何保存图像 - How to save an image when button is clicked 单击UICollectionViewCell时更改按钮背景图像 - Changing button background image when clicked UICollectionViewCell 尝试制作一个屏幕快照按钮,单击该按钮会将屏幕快照发送到我的相机胶卷。 Swift 3,Xcode 8,IOS - Trying to make a screenshot button that sends the screenshot to my camera roll, when the button is clicked. Swift 3, Xcode 8, IOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM