我尝试了很多方法,但无法让它发挥作用...
[[NSUserDefaults standardUserDefaults] setObject:[my_popup_button selectedItem] forKey:@"selected"];
不起作用,有什么建议吗?
最佳答案
这是我刚刚创建的一个最小示例:
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"];
}
刚刚尝试了一下,效果非常好。 最好的,
弗洛
关于objective-c - 使用 NSUserDefaults 保存从 NSPopupbutton 中选择的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038672/