1
我想用NSString的數組填充NSPopUpButton。我也希望能夠在NSPopUpButton中設置選定的項目並獲取選定的值。有沒有辦法使用綁定來做到這一點?以下是我所擁有的只有數組控制器的內容綁定到arraedObjects的內容。將NSPopUpButton綁定到NSArray
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
NSMutableArray *myArray;
IBOutlet NSPopUpButton *myPopUpButton;
IBOutlet NSArrayController *processArrayController;
}
@property (assign) IBOutlet NSWindow *window;
@end
@implementation AppDelegate
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *firstObject = @"Lustre";
NSString *secondObject = @"TwoTone Laser";
NSString *thirdObject = @"Laser Mark";
NSString *forthObject = @"Double Lustre";
NSString *fifthObject = @"CG Ink";
// Create the array
myArray = [[NSMutableArray alloc] initWithObjects:firstObject, secondObject,
thirdObject, forthObject, fifthObject, nil];
// Sort the array
[myArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
// Set contents of the array controller that is bound to the popup button
[processArrayController setContent:myArray];
// Set a selection to an item of the popup button
[myPopUpButton selectItemWithTitle: firstObject];
}
@end