I am having trouble figuring out how to properly use the NSPopUpButton class with pop-up menus.
I have the basic working. I can display, select and save my selection from the pop-up window in my NSPopUpButton versionsPopupOutlet object (which is part of my PreferenceController object).
The PreferenceController object is my Preferences window for the program.
I can close and open up again the Preferences window and it shows my last selection! Great!
But when I quit and run my program again, the NSPopUpButton versionsPopupOutlet object does NOT show my last saved selection (which the program will ultimately use). It always shows the first item in the pop-up menu of the NSPopUpButton versionsPopupOutlet object (which may or may not be correct).
The code is simple.
PreferenceController.h is:
#import <AppKit/AppKit.h>
@interface PreferenceController : NSWindowController
{
IBOutlet NSPopUpButton *versionsPopupOutlet;
}
- (IBAction)save:(id)sender;
- (IBAction)close:(id)sender;
@end
PreferenceController.m is:
#import "PreferenceController.h"
@implementation PreferenceController
- (id)init
{
self = [super initWithWindowNibName:@"Preferences"];
return self;
}
- (void)windowDidLoad
{
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *commonlist = @"/bin/powerpc-apple-darwin:/usr/X11R6/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/sw/bin:/sw/sbin:/opt/local/bin"; // Added /opt/local/bin
NSString *pathlist = [environment objectForKey:@"PATH"];
NSArray *commonItems = [commonlist componentsSeparatedByString:@":"];
NSArray *pathItems = [pathlist componentsSeparatedByString:@":"];
NSString *path;
BOOL duplicate;
unsigned int i, h;
for (i = 0; i < [commonItems count]; i++)
{
path = [[commonItems objectAtIndex:i] stringByAppendingString:@"/lame"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
[versionsPopupOutlet addItemWithTitle:path];
}
for (i = 0; i < [pathItems count]; i++)
{
duplicate = FALSE;
for (h = 0; h < [commonItems count]; h++)
{
if ([[pathItems objectAtIndex:i] isEqualToString:[commonItems objectAtIndex:h]])
duplicate = TRUE;
}
if (!duplicate)
{
path = [[pathItems objectAtIndex:i] stringByAppendingString:@"/lame"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path])
[versionsPopupOutlet addItemWithTitle:path];
}
}
}
- (IBAction)save:(id)sender
{
if ([versionsPopupOutlet indexOfSelectedItem] == 0)
[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"lamePath"];
else
[[NSUserDefaults standardUserDefaults] setObject:[versionsPopupOutlet titleOfSelectedItem] forKey:@"lamePath"];
[self close];
}
- (IBAction)close:(id)sender
{
[[self window] close];
}
@end
In my main GUI program, I have:
+ (void)initialize
{
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
[defaultValues setObject:@"" forKey:@"lamePath"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
}
As you can see, the value for the pop-up menu is stored in a preference file via [NSUserDefaults standardUserDefaults] setObject: forKey: method in the (IBAction)save:(id)sender method shown above. The question is how do I set the pop-up menu to display the last saved item from the pop-up menu from the saved preference file?
I can obtain the last saved value in the preference file using [[NSUserDefaults standardUserDefaults] objectForKey:@"lamePath"]. But how does one tell the popup menu to display this last saved value when I run the Preference window is first displayed?