使用NSUserDefaults从NSPopup按钮保存所选项目

时间:2013-08-04 00:35:46

标签: objective-c macos cocoa nspopupbutton

我尝试了很多东西,无法让它发挥作用......

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

不起作用,有什么建议吗?

1 个答案:

答案 0 :(得分:3)

这是我刚刚创建的一个最小例子:

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"];
}

尝试过它就像魅力一样。 最好,

弗洛