Question & one Answer for NSButton & activation/click sound when in Toggle mode. Better answer?
Question & one Answer for NSButton & activation/click sound when in Toggle mode. Better answer?
- Subject: Question & one Answer for NSButton & activation/click sound when in Toggle mode. Better answer?
- From: tyler <email@hidden>
- Date: Mon, 6 Aug 2001 22:36:43 -0700
Yo!
I'm using an NSButton in toggle mode (click to toggle state between two
states, like a check box). Using the sound "feature" of NSButton to
play a sound on click (sound is a "click" sound).
However, the sound only plays when toggling one way, but not the other
(which makes sense for non-toggle type behavior of button, but not much
sense for toggle type behavior - perhaps this is an implementation
oversight (bug)).
Any easy way to get sound to play on both clicks without subclassing
NSButton or NSButtonCell?
(I guess I can subclass NSButtonCell and override
startTrackingAt:inView:, unless someone out there has a better idea...?)
OK. Here's an answer - just make my target of the button handle the
sound... seems easier, but a bit less elegant than having the button do
it.
anybody got a better fix?
tyler
//
-----------------------------------------------------------------------------------
Ok. So here's the implementation that has the NSButton target handle
the sound
in case anyone might find it useful. Shockingly simple (let's hear it
for Cocoa!).
// In my target object (controller for window) I have a method that is
the action method of the NSButton:
- (IBAction)doSwitch:(id)sender
{
[self playSwitchSound]; // play sound each time it's hit.
// ... do other stuff here in response to button being clicked upon.
}
// A simple initialization in awakeFromNib to pre-load the sound:
- (void) awakeFromNib
{
NSString * path = [[NSBundle mainBundle] pathForSoundResource:
@"click.aiff"];
clickSnd = [[NSSound alloc] initWithContentsOfFile: path
byReference: YES]; // byRef ??
// anyone know what the "byReference" param is for/does?
}
// and the method to actually play the sound
- (void) playSwitchSound
{
BOOL b;
if ( clickSnd )
b = [clickSnd play];
// Anyone know what the boolean return means?
}