• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSButton state not persisting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSButton state not persisting


  • Subject: NSButton state not persisting
  • From: Pax <email@hidden>
  • Date: Tue, 10 Dec 2013 20:00:12 +0000

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@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: NSButton state not persisting
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Re: AutoLayout and and views not appearing after segues.
  • Next by Date: Re: wantsDefaultClipping set to NO
  • Previous by thread: Re: NSURLSessionUploadTask - stops uploading after sending 160kB of data (5x 32kB chunks)
  • Next by thread: Re: NSButton state not persisting
  • Index(es):
    • Date
    • Thread