[英]How to change image of UIImageView that is inside a UIButton programmatically?
[英]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.