我有 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
因此,如果您在您的超级视图(所有 UIImageView 都驻留在其中)上调用此方法,您应该这样做:
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 个 IBOutlets 工作量太大,我建议您也以编程方式创建 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!