【问题标题】:how to change uiimageview image's programmatically如何以编程方式更改 uiimageview 图像
【发布时间】:2013-04-26 13:16:45
【问题描述】:

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

【问题讨论】:

标签: ios objective-c xcode uiimageview uiimage


【解决方案1】:

试试这样,

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

【讨论】:

  • 我做了同样的代码,但我得到了这个错误:-[UIRoundedRectButton setImage:]: unrecognized selector sent to instance 0x8993130
  • 好的问题是标签值按钮和图像具有相同的标签值。
  • 在self.view的地方替换scrollView。
  • 为图像和按钮提供不同的标签值,然后它就可以工作了。
【解决方案2】:

而不是这个:

imageView.tag=UrunId;

写:

imageView.tag = productID + 100;

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

然后实现buttonAction:like:

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

【讨论】:

  • 因为@Sunny 在您几秒钟前给出了答案。再次感谢。
【解决方案3】:

请尝试使用这个

按钮和图片应该有不同的标签。所以请先给出这样的不同标签....

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-16
    • 2015-08-15
    • 1970-01-01
    • 2011-04-18
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2021-01-14
    相关资源
    最近更新 更多