Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lost in NSButton types and alternate images

I’d like to have an NSButton with an image and an alternate image. The alternate image should be displayed while the button is being pressed and I’d also like to display the alternate image from code, calling something like [button setSelected:YES]. Is this possible without monkeing with the alternateImage property by hand?

like image 722
zoul Avatar asked Mar 16 '11 13:03

zoul


3 Answers

This is possible without manually changing the button's image:

In Interface Builder (xib/nib Editor) set the Type of NSButton to "Toggle" and the image will automatically change to the alternate image/title.

Set the type to Toggle to use the alternate image

like image 72
auco Avatar answered Nov 06 '22 02:11

auco


You can use a NSButton with type set to NSToggleButton and then switch between the image and the alternateImage using the NSOnState / NSOffState states of the NSButton.

NSButton* theButton = [[NSButton alloc] initWithFrame:....];
theButton.image = .....
theButton.alternateImage = .....
theButton.state = NSOffState; // displays the image
theButton.state = NSOnState; // displays the alternateImage
like image 5
yonel Avatar answered Nov 06 '22 00:11

yonel


The easiest way is to switch between the two images:

@implementation NSButton (Select)

- (void) setSelected: (BOOL) yn
{
    NSImage *const tmp = [self image];
    [self setImage:[self alternateImage]];
    [self setAlternateImage:tmp];
}

@end
like image 3
lbrndnr Avatar answered Nov 06 '22 01:11

lbrndnr