繁体   English   中英

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

[英]how to change uiimageview image's programmatically

在我的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标记传递给imgview.tag

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

而不是这个:

imageView.tag=UrunId;

写:

imageView.tag = productID + 100;

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

然后实现buttonAction: like:

- (void)buttonAction:(UIButton *)sender
{
  UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:(sender.tag+100)];
  [imageView setImage:yourImage];
}

请尝试使用这个

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

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM