ios - 如何以编程方式更改 uiimageview 图像

标签 ios objective-c xcode uiimageview uiimage

在我的 ios 应用程序中,我以编程方式创建 uiimageviews,

for (int i=0; i<20; i++) {

                                   productID=[products objectAtIndex:i];

                                   UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                                   [button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
                                   //[button setImage:redButtonImage forState:UIControlStateNormal];
                                   [button addTarget:self
                                              action:@selector(buttonAction:)
                                    forControlEvents:UIControlEventTouchUpInside];
                                   button.tag = productID;
                                   [scrollView addSubview:button];



                                   UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
                                   imageView.tag=productID;
                                   UIImage *image = [UIImage imageNamed:@"redImage.png"];
                                   [imageView setImage:image];
                                   [scrollView addSubview:imageView];


                               }

在我的 buttonActon 中,我想将该 uiimageview 的图像更改为另一个图像。但我不能那样做。谁能帮我?谢谢。

最佳答案

这样试试,

for (int i=0; i<20; i++) {

                                   productID=[products objectAtIndex:i];

                                   UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                                   [button setFrame:CGRectMake(column*197+38 , row*350+8, 159, 45)];
                                   //[button setImage:redButtonImage forState:UIControlStateNormal];
                                   [button addTarget:self
                                              action:@selector(buttonAction:)
                                    forControlEvents:UIControlEventTouchUpInside];
                                   button.tag = 100+productID;
                                   [scrollView addSubview:button];



                                   UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(column*197+170, row*350+25, 13, 13)];
                                   imageView.tag=productID;
                                   UIImage *image = [UIImage imageNamed:@"redImage.png"];
                                   [imageView setImage:image];
                                   [scrollView addSubview:imageView];


                               }

将这个保留在按钮操作方法中,并在 imgview.tag 的位置传递 imageview 标签

     UIImageView *img=(UIImageView *)[scrollView viewWithTag:imgview.tag];
    img.image=[UIImage imageNamed:@"name.png"];

关于ios - 如何以编程方式更改 uiimageview 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16237443/

相关文章:

ios - 子类化的 UIScrollView

ios - 从其他应用程序和 NotificationCenter 接收有意义的通知

ios - 如何在 Swift 中使用 SKPSMTPMessageDelegate 协议(protocol)?

IOS UIButton 不响应触摸,需要多次按下,UIScreenEdge 手势干扰?

ios - Xcode 9 : Getting More Information On Memory Leak

iphone - EXC_BAD_ACCESS 错误,内存管理问题

ios - 无法使用自定义 CA 打开安全的 websocket

ios - 在 Xcode 中,有没有办法绕过一行或多行的 "All Exceptions"断点?

objective-c - Javascript 到 Obj-c 请求.. 将值返回给 Javascript

iphone - NSString 超出范围问题?