iOS:使用标签值更改UIImageView [英] IOS: change UIImageView with tag value
问题描述
我有20个UIImageView,我想更改其图像;但是我不想创建20个IBOutlet,并且我想使用标签值来更改图像;我在接口构建器中设置标签值,然后呢?如果要更改Imageview号为15的图像?我该怎么办?
I have 20 UIImageView and i want change their image; but I don't want to create 20 IBOutlet, and I want to use tag value to change the image; I set the tag value in interface builder and after? If I want to change image at Imageview number 15? How can I do?
推荐答案
如果使用标签,您可以像下面这样标识您的标签视图(UIImageView是UIView的子类,具有标签属性):
If you use tags you can identify your tagged view (UIImageView is a subclass of UIView, which has the tag attribute) like this:
- (UIView *)viewWithTag:(NSInteger)tag
因此,如果您在超级视图(所有UIImageViews都驻留在其上)上调用此方法,则应该这样:
So if you call this method on your superview (which all the UIImageViews reside in), you should do it like this:
UIImageView *myImageView = (UIImageView *)[myAwesomeSuperview viewWithTag:15];
(文档我使用Google找到).
(Documentation I found using Google).
P.S:如果您认为添加20个IBOutlet太繁琐,建议您也以编程方式创建UIImageViews.这样,您将完全不需要xib文件,只需编写一小段代码,并以更少的努力获得更好的维护.
P.S: if you think it is too much work to add 20 IBOutlets, I recommend you create the UIImageViews programmatically as well. This way you will not need a xib file at all, write a small piece of code and have better maintenance with less effort.
这篇关于iOS:使用标签值更改UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!