Question

I've tried numerous things and cannot get it to function...

 [[NSUserDefaults standardUserDefaults] setObject:[my_popup_button selectedItem] forKey:@"selected"];

Does not work, got any suggestions?

Was it helpful?

Solution

This is from a minimal example I just created:

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSPopUpButton *popupButton;
- (IBAction)onPopupBtnSelectedItemChanged:(id)sender;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self.popupButton selectItemAtIndex:[[NSUserDefaults standardUserDefaults] integerForKey:@"selectedItemIndex"]];
}


- (IBAction)onPopupBtnSelectedItemChanged:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setInteger:self.popupButton.indexOfSelectedItem                            
                                               forKey:@"selectedItemIndex"];
}

Just tried it and it works like a charm. Best,

Flo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top