Binding 将NSPoupButton绑定到NSArray
我想用NSString数组填充NSPoupButton。我还希望能够在NSPopUpButton中设置所选项目,并获取所选值。有没有办法使用绑定来实现这一点?这里是我所拥有的,它只将数组控制器的内容绑定到arragedObjectsBinding 将NSPoupButton绑定到NSArray,binding,nsarray,nsarraycontroller,nspopupbutton,Binding,Nsarray,Nsarraycontroller,Nspopupbutton,我想用NSString数组填充NSPoupButton。我还希望能够在NSPopUpButton中设置所选项目,并获取所选值。有没有办法使用绑定来实现这一点?这里是我所拥有的,它只将数组控制器的内容绑定到arragedObjects @interface AppDelegate : NSObject <NSApplicationDelegate> { NSMutableArray *myArray; IBOutlet NSPopUpButton *myPopUpBut
@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
在应用程序控制器中设置ivar以保存您的选择:
@property (copy) NSString *selection;
self.selection = firstObject;
当然,在您的实现文件中综合它
将以下绑定到您的NSPopUpButton实例:
内容:
绑定到:阵列控制器,除非已为阵列控制器指定了其他名称
控制器键:arrangedObjects
内容值:
绑定到:阵列控制器,除非已为阵列控制器指定了其他名称
控制器键:arrangedObjects
模型密钥路径:对于字符串,我总是使用“description”
所选对象:
绑定到:应用程序委派,除非您已为应用程序委派指定了其他名称
模型关键路径:自选择
最后,由于您的弹出按钮现在已绑定到选择,因此这是您设置初始选择的方式:
@property (copy) NSString *selection;
self.selection = firstObject;
祝你在努力中好运