2013-04-26 12 views
4

在我的iPhone應用程序我創建UIImageViews的編程那樣,如何在我的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 = 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]; 


           } 

,我想是的UIImageView的形象改變到另一個圖像。但我不能那樣做。誰能幫我?謝謝。

+1

你不能這樣做,因爲? [你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Pfitz 2013-04-26 13:18:53

回答

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

我做了相同的代碼,但我得到這個錯誤: - [UIRoundedRectButton setImage:]:無法識別選擇器發送到實例0x8993130 – bdkhsn 2013-04-26 13:23:04

+0

那是什麼錯誤? – Balu 2013-04-26 13:23:23

+0

現在檢查我編輯我的答案,我忘了*符號。 – Balu 2013-04-26 13:24:38

3

而不是這個地方:

imageView.tag=UrunId; 

寫:

imageView.tag = productID + 100; 

因爲您需要每個圖像視圖的唯一標籤。

然後實現buttonAction:,如:

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

非常感謝。這是我的解決方案。 – bdkhsn 2013-04-26 13:29:17

+0

@bdkhsn:謝謝你的評論:) – 2013-04-26 13:39:36

+0

因爲@Sunny在幾秒前給出了答案。再次感謝。 – bdkhsn 2013-04-29 11:43:54

2

請儘量使用這一個

你應該有不同標籤的按鈕和image.So請給這樣的第一不同的標籤....

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

} 
相關問題