0

I created a UIButton with UIImageView and UILabel by the help of this question and answer by Tim and now I want change the image of the UIImageView on the button click event. I tried [sender.imageView setImage:image] but it not working

Cœur
  • 37,241
  • 25
  • 195
  • 267
Varun Naharia
  • 5,318
  • 10
  • 50
  • 84

1 Answers1

2

you can:

1 - keep a reference to the UIImageView inside the UIButton on your viewController

2 - Subclass the UIButton and add the UIImageView yourself.

3 - When adding the UIImageView, add a tag into it (view.tag) and when getting the view from the sender, you can just

UIView *view = (UIView *)sender;
UIImageView *imageView = [view viewWithTag:123];
//do what you must with the imageView.

The tag can be any number.

Edit

this is how you should set the tag on the UIImageView, and Not on the UIButton. I copied this code from Tim's answer from here:

// Create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

// Now load the image and create the image view
UIImage *image = [UIImage imageNamed:@"yourImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(/*frame*/)];
[imageView setImage:image];

// Create the label and set its text
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(/*frame*/)];
[label setText:@"Your title"];

// Set a tag on the UIImageView so you can get it later
imageView.tag = 123;

// Put it all together
[button addSubview:label];
[button addSubview:imageView];
Community
  • 1
  • 1
Erakk
  • 922
  • 5
  • 13
  • No I don't want to change the image of button but the UIImageView inside that button, have you read this http://stackoverflow.com/questions/1116114/uibutton-title-and-image-alignment-query/1116139?noredirect=1#comment49224486_1116139 question and Tim's answer ?? – Varun Naharia Jun 01 '15 at 16:02
  • here 123 is the same tag that set earlier or it is new ? – Varun Naharia Jun 01 '15 at 16:41
  • It must be the same tag you set earlier. I used "123" as an example. If you set 700 before, you need to use 700 here, for example. – Erakk Jun 01 '15 at 16:43
  • application crash at `UIImageView *imageView = [button viewWithTag:123];` Incompatible pointer UIImageView with type UIView – Varun Naharia Jun 02 '15 at 01:49
  • are you using 123 as the tag when setting the view? That error means that the view being fetched is not a UIImageView – Erakk Jun 02 '15 at 12:20
  • No I am not using tag 123 but I did replace the 123 with the real tag value, button is not a UIImage type it is UIButton type but I added a subview to that button which was a UIImage type and I want to change image of that UIImage – Varun Naharia Jun 02 '15 at 12:27
  • Well, UIImage != UIImageView. I edited the answer to be more generic. You cannot add UIImage as a subview, you must add UIImageView which also contains an UIImage. I just tested this and it is working. – Erakk Jun 02 '15 at 16:00
  • you did't understand I can't set any tag on UIImageView the tag which I have is for UIButton – Varun Naharia Jun 02 '15 at 16:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79444/discussion-between-erakk-and-varun-naharia). – Erakk Jun 02 '15 at 16:25