Pergunta

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?

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top