4

在我的 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 的图像更改为另一个图像。但我不能那样做。谁能帮我?谢谢你。

4

3 回答 3

12

试试这样

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


                               }

将此保留在按钮操作方法中并传递 imageview 标签代替imgview.tag

     UIImageView *img=(UIImageView *)[scrollView viewWithTag:imgview.tag];
    img.image=[UIImage imageNamed:@"name.png"];
于 2013-04-26T13:19:29.307 回答
3

而不是这个:

imageView.tag=UrunId;

写:

imageView.tag = productID + 100;

因为每个图像视图都需要一个唯一的标签。

然后实现buttonAction:类似:

- (void)buttonAction:(UIButton *)sender
{
  UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:(sender.tag+100)];
  [imageView setImage:yourImage];
}
于 2013-04-26T13:23:12.590 回答
2

请尝试使用这个

你应该有不同的按钮和图像标签。所以请给第一个这样的不同标签....

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

之后这样做...

- (void)buttonAction:(UIButton *)sender
{
  UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:sender.tag+1];
  imageView.image=[UIImage imageNamed:@"imageName"];

}
于 2013-04-26T13:26:18.563 回答