Question

I have this method for a twitter button

 -(IBAction)tweetThis:(id ) sender{

    if ([sender isSelected]) {
        [sender setImage:[UIImage imageNamed:@"TwitterButtonBlue.png"] forState:UIControlStateNormal];
        [sender setSelected:NO];
    }
    else {
        [sender setImage:[UIImage imageNamed:@"TwitterButtonBlack.png"] forState:UIControlStateSelected];
        [sender setSelected:YES];}

    }

in the simulator when i tap the button (which I made in IB as a touch up inside) the button clicks and changes to images but it doesn't go back to the original button image so for instance the blue.png changes to black.png but stays black and doesn't go back to blue. Any way I canf ix this

Was it helpful?

Solution

If I understand correctly, you're just trying to change the button image when the button is tapped. If so, you can simply just configure the button (in viewDidLoad or interface builder) like this:

[button setImage:[UIImage imageNamed:@"TwitterButtonBlue.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"TwitterButtonBlack.png"] forState:UIControlStateSelected];

The UIButton will handle the image switching for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top