views:

5491

answers:

5

Hello, I want to change the image on UIButton, when User presses that button. So, I have write

btnthumbnail2 setImage:[UIImage imageNamed:@"leaderboard_ov.png"] forState:UIControlStateNormal];

and after that I change the view.

The image is changing but not display the changed image.I think the controll goes to next view tha's why it is happening. But I want to show the change, How can I do this?? Plz help me for this..

+2  A: 

To change the image for the selected state you have to call setImage for state UIControlStateSelected. You can set separate images for different states (Normal, Highlighted, Disabled, Selected, etc).

Ramin
+1  A: 

The code you have there should work fine, but I'm guessing that the button image doesn't appear changed until the next run through the event loop, so you don't see it before the view gets switched out.

It's kind of a hack, but you could try delaying the view change just slightly so that the button will update.

So instead of this:

[btnthumbnail2 setImage:[UIImage imageNamed:@"leaderboard_ov.png"] forState:UIControlStateNormal];
[self showMyOtherView];

Try this:

[btnthumbnail2 setImage:[UIImage imageNamed:@"leaderboard_ov.png"] forState:UIControlStateNormal];
[self performSelector:@selector(showMyOtherView) withObject:self afterDelay:0.01];
zpasternack
thanks It is working ...
A: 

I wrote code that does exactly this last night, and I didn't have to resort to any strange delaying tactics. In this particular case, I'm doing it from inside a tableViewCell. This is more code than you asked for, but it shows the whole process I used. As you can see, I chose not to have any special image for the period of time DURING a button touch.

// Button set up, inside cellForRowAtIndexPath:
// imageFrame is just the rect that you want the button to occupy
  [self setFavorite:[[UIButton alloc] initWithFrame:imageFrame]];
  [[self favorite] addTarget:self action:@selector(toggleFavorite:)
        forControlEvents:UIControlEventTouchUpInside];
  [cell addSubview:[self favorite]];
  [self draw];


// definition of the callback specified above
- (void) toggleFavorite:(id) sender {
  if (favoriteState == 0 ){
    favoriteState = 1;
  } else {
    favoriteState = 0;
  }
  [self draw];
}

// the draw method, to set the images
// favOn and favOff are statically defined at the top of the class
- (void) draw {
  if (favoriteState != 0) {
    [[self favorite] setBackgroundImage:favOn forState:UIControlStateNormal];
    [[self favorite] setBackgroundImage:favOn forState:UIControlStateHighlighted];
  } else {
    [[self favorite] setBackgroundImage:favOff forState:UIControlStateNormal];
    [[self favorite] setBackgroundImage:favOff forState:UIControlStateHighlighted];
  }
}
mmc
A: 

i know the anwer lol, its pretty simple. your image is named "leaderboard ov.png" right?

its not "leaderboard_ov.png"

in images you dont do "_" instead of spaces

Justin
A: 

Hi, Here is how I solved it. I am using IB in which I added a UIButton to the view.

  1. I defined the button selected by default and selected my image for the selected state (Use drope-down list in inspector window to choose "Selected State Configuration" before selecting the image).

  2. Create an IBAction in the controller and connect the button to that action.

  3. Then see the code below:

    -(IBAction) toggleUIButtonImage:(id)sender{
     if ([sender isSelected]) {
         [sender setImage:unselectedImage forState:UIControlStateNormal];
         [sender setSelected:NO];
     }else {
         [sender setImage:selectedImage forState:UIControlStateSelected];
         [sender setSelected:YES];
    

    }}

I think this is quite a smooth solution. Hope it helps!

Nicsoft
thanks nicsoft, this helped me figure out one piece of the puzzle!
iWasRobbed