开发者

Lost in NSButton types and alternate images

I’d like to have an NSButton w开发者_StackOverflow中文版ith 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?


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.

Lost in NSButton types and alternate images


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


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


I just built all 10 types of buttons in the interface editor's attribute inspector with no added coding. Here are the results:

button type while held down when released
Momentary Push-In first image darkened first image
Momentary Light first image darkened first image
Momentary Change ALT IMAGE first image
Push On / Push Off first image darkened first image
On / Off first image darkened first image
Toggle ALT IMAGE ALT IMAGE
Switch ALT IMAGE ALT IMAGE
Radio (not valid)
Accelerator first image darkened first image
Multi Level Acc. first image darkened first image

Toggle and Switch will revert to the original image when pressed again. (If you change the button type to Radio in the button's Attribute Inspector, it reverts to Switch.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜