UIImage Buttons (Rounded Rect Button)

Go To StackoverFlow.com

0

I want to be able to change the image of any of the buttons upon the user pressing the button and then when they release, the image that was there before is put back.

So basically when they press their finger on the button, the image changes and when they release, the original image I specified in IB is put back.

How can I do this? Here is the code I have so far.

- (IBAction)btnOne:(id)sender {
    result.text = [self appendresult:@"1"];

    //Changes the button image upon touch
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1.png"] forState:UIControlStateNormal]; 
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1Alt.png"] forState:UIControlStateSelected];
    btnOne.showsTouchWhenHighlighted = YES;
}
2012-04-04 01:38
by user1311669
I see that you are new to StackOverflow, If this answer has helped you, then please mark it as correct - Mick MacCallum 2012-04-04 02:20


1

Put your code...

  [btnOne setImage:[UIImage imageNamed:@"NumberBtn1.png"] forState:UIControlStateNormal]; 
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
    [btnOne setImage:[UIImage imageNamed:@"NumberBtn1Alt.png"] forState:UIControlStateSelected];
    btnOne.showsTouchWhenHighlighted = YES;

in viewDidLoad instead of in the IBAction, then just make sure that your button in Interface Builder is properly linked to the IBOutlet you created for it.

2012-04-04 01:49
by Mick MacCallum