IOS:使用标签值更改 UIImageView

发布于 2024-11-04 02:08:37 字数 113 浏览 4 评论 0原文

我有 20 个 UIImageView,我想更改它们的图像;但我不想创建20个IBOutlet,我想使用标签值来更改图像;我在界面生成器中设置了标签值,然后?如果我想更改图像视图编号 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

木落 2024-11-11 02:08:37
UIImageView *imageView=(UIImageView *)[self.view viewWithTag:*givetag*];
[imageView setImage:[UIImage imageNamed:@"nameof yourimage"]];
UIImageView *imageView=(UIImageView *)[self.view viewWithTag:*givetag*];
[imageView setImage:[UIImage imageNamed:@"nameof yourimage"]];
避讳 2024-11-11 02:08:37

如果您使用标签,您可以识别您的标记视图(UIImageView 是 UIView 的子类,它具有 tag 属性),如下所示:

- (UIView *)viewWithTag:(NSInteger)tag

因此,如果您在超级视图(所有 UIImageView 都驻留在其中)上调用此方法,您应该这样做这个:

UIImageView *myImageView = (UIImageView *)[myAwesomeSuperview viewWithTag:15];

文档我使用 Google 找到的)。

PS:如果您认为添加 20 个 IBOutlet 工作量太大,我建议您也以编程方式创建 UIImageViews。这样您就根本不需要 xib 文件,只需编写一小段代码,就能事半功倍地获得更好的维护。

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

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];

(Documentation I found using Google).

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.

李白 2024-11-11 02:08:37

SWIFT,2015 年 4 月:

func createUIImageViews()
{
    for i in 0...99
    {
        var imageView:UIImageView = UIImageView(frame: CGRectMake(CGFloat(0), CGFloat(0), CGFloat(100), CGFloat(100)))
        imageView.userInteractionEnabled = true
        imageView.tag = i
        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "onImageViewTap:"))

        self.view.addSubview(imageView)
    }
}



func onImageViewTap(sender:UITapGestureRecognizer)
{
    println("TAP: \(sender.view!.tag)")
}

SWIFT, April 2015:

func createUIImageViews()
{
    for i in 0...99
    {
        var imageView:UIImageView = UIImageView(frame: CGRectMake(CGFloat(0), CGFloat(0), CGFloat(100), CGFloat(100)))
        imageView.userInteractionEnabled = true
        imageView.tag = i
        imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "onImageViewTap:"))

        self.view.addSubview(imageView)
    }
}



func onImageViewTap(sender:UITapGestureRecognizer)
{
    println("TAP: \(sender.view!.tag)")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文