4

In my ios application i create uiimageviews programmatically like that,

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


                               }

in my buttonActon, i want to change that uiimageview's image to another image. But i can not do that. Can anyone help me? Thank You.

1
  • 1
    You can not do this because? What have you tried? Commented Apr 26, 2013 at 13:18

3 Answers 3

12

try like this ,

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


                               }

keep this one in button action method and pass the imageview tag in the place of imgview.tag

     UIImageView *img=(UIImageView *)[scrollView viewWithTag:imgview.tag];
    img.image=[UIImage imageNamed:@"name.png"];
Sign up to request clarification or add additional context in comments.

4 Comments

i did same code but i got this error :-[UIRoundedRectButton setImage:]: unrecognized selector sent to instance 0x8993130
ok problem is that with tag value both the button and image having same tag value.
in the place self.view replace scrollView.
give different tag values for both images and buttons then it'l work.
3

Instead of this:

imageView.tag=UrunId;

Write:

imageView.tag = productID + 100;

Because you need a unique tag for each Image View.

Then implement the buttonAction: like:

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

1 Comment

because @Sunny gives the answer a few seconds ago before you. thanks again.
2

Please try to use this one

You should have different tag for button and image.So please give first different tag like this....

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

After that do this...

- (void)buttonAction:(UIButton *)sender
{
  UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:sender.tag+1];
  imageView.image=[UIImage imageNamed:@"imageName"];

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.