Ambreen
Ambreen

Reputation: 37

I want to switch button image on click

I want to change the image of button on click. click once 1 image should be set on he button and when again clicked the 2nd image should be there and these images should switch on click.

Upvotes: 0

Views: 2738

Answers (1)

André Hoffmann
André Hoffmann

Reputation: 3553

Add a button to your view, remove the title and set the type to toggle(in the attributes tab of the inspector). Here also set the image and alternate image of your button like this:

attributes inspector http://img340.imageshack.us/img340/2310/bildschirmfoto20090928u.png

That should do it.

If you want to use a custom image you will have to do it programatically like this:

NSString* path  = [[NSBundle mainBundle] pathForResource:@"myImage" 
                              ofType:@"png"];
NSURL* url      = [NSURL fileURLWithPath:path];
NSImage *image  = [[NSImage alloc] initWithContentsOfURL: url];

[myButton setImage: image];

and respectively for the alternate image:

[myButton setAlternateImage: image2];

Upvotes: 9

Related Questions