在我的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的图像更改为另一个图像。但我不能这样做。谁能帮我?谢谢。
答案 0 :(得分: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];
}
将此视频保留在按钮操作方法中,并在imgview.tag
UIImageView *img=(UIImageView *)[scrollView viewWithTag:imgview.tag];
img.image=[UIImage imageNamed:@"name.png"];
答案 1 :(得分:3)
而不是:
imageView.tag=UrunId;
写:
imageView.tag = productID + 100;
因为每个图像视图都需要一个唯一标记。
然后实现buttonAction:
之类的:
- (void)buttonAction:(UIButton *)sender
{
UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:(sender.tag+100)];
[imageView setImage:yourImage];
}
答案 2 :(得分: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"];
}