Another weird one (or another paper bag error from me!)
I'm writing a pref pane and, when it loads, it checks to see if the application
that it provides the settings for is running or not. If the application is
running then it sets an NSButton to On, and if it isn't then it sets the button
to Off. This button can then be used to quit or load the application as
appropriate.
Oddly, though, the state of the button doesn't seem to persist.
I set the state as follows (I've tried this code in mainViewWillLoad,
willSelect and awakeFromNib):
if ([self applicationIsRunning])
{
[_onOffSwitch setState:NSOnState];
}
else
{
[_onOffSwitch setState:NSOffState];
}
[self setActive:_onOffSwitch];
The code of setActive is as follows - obviously this doesn't do anything useful
yet, I'll hook up the code to load and quit the application once I've got this
going correctly:
- (IBAction)setActive:(id)sender
{
NSImage* buttonImage;
if ([sender state]==NSOnState)
{
NSLog(@"State is ON");
buttonImage = [[NSImage alloc] initWithContentsOfFile:[NSString
stringWithFormat:@"%@/Contents/Resources/SwitchRight.tiff",bundlepath]];
}
else if ([sender state]==NSOffState)
{
NSLog(@"State is OFF");
buttonImage = [[NSImage alloc] initWithContentsOfFile:[NSString
stringWithFormat:@"%@/Contents/Resources/SwitchLeft.tiff",bundlepath]];
}
[sender setImage:buttonImage];
[sender setAlternateImage:buttonImage];
}
The problem is that the state of _onOffSwitch doesn't seem to persist. If I
put the code in awakeFromNib then it gets called twice (a known feature of
awakeFromNib) - once with the correct value and the second time with NSOffState
(regardless of whether the application is running or not). If I put the code
in willSelect or mainViewWillLoad then it gets called once - but aways with
NSOffState. The value that I assign seems to be forgotten.
The function aways returns the correct result, so the problem is either with my
button or with setActive.
The button is declared as
@property (assign) IBOutlet NSButton* onOffSwitch;
Any ideas?
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]