带有绑定的 NSComboBox 和 NSTextField

发布于 2024-11-19 16:20:43 字数 266 浏览 2 评论 0原文

我有一个 NSArrayController,我将其填充到 awakeFromNib 方法中。数据具有键:idnamedescription。我有一个 ComboBox 和一个 TextField 绑定到 NSArrayController,第一个带有名称,第二个带有 id。如果我更改组合框中的选择,我希望文本字段中的值发生更改,反之亦然。我阅读了 TextField 和 ComboBox 绑定的文档,但我不明白如何实现这一点。

I have an NSArrayController that I fill in the awakeFromNib method. The data has the keys: id, name, and description. I have a ComboBox and a TextField bound to the NSArrayController the first with the name and the second with the id. If I change the selection in the ComboBox I want the value in the TextField to change, and vice-versa. I read the docs for TextField and ComboBox bindings, but I didn't understand how to achieve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单调的奢华 2024-11-26 16:20:43

这里的技巧是您需要在其他地方放置 NSComboBox 的值。 NSArrayController 可以很好地向 NSComboBox 提供库存值,但是您可以在 NSComboBox 中键入可能不在 NSArrayController 的 contentArray 中的任意值,因此您需要在其他地方放置该值也就不足为奇了。我可以通过在 AppDelegate 上放置一个简单的值来快速模拟它,如下所示:

@interface SOAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
// The NSArrayController you were talking about...
@property (assign) IBOutlet NSArrayController* popupValues;    
// The other place to store data...
@property (retain) id comboBoxValue;

@end

然后在实现中:

@implementation SOAppDelegate

@synthesize window = _window;
@synthesize comboBoxValue = _comboBoxValue;

- (void)dealloc
{
    [_comboBoxValue release];
    _comboBoxValue = nil;
    [super dealloc];
}

-(void)awakeFromNib
{    
    [super awakeFromNib];
    NSMutableDictionary* item1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                                  [NSNumber numberWithUnsignedInteger: 1], @"id", 
                                  @"Item 1 Name", @"name", 
                                  @"Item 1 Description", @"description", 
                                  nil];
    NSMutableDictionary* item2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                                  [NSNumber numberWithUnsignedInteger: 2], @"id", 
                                  @"Item 2 Name", @"name", 
                                  @"Item 2 Description", @"description", 
                                  nil];
    NSMutableDictionary* item3 = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                                  [NSNumber numberWithUnsignedInteger:3], @"id", 
                                  @"Item 3 Name", @"name", 
                                  @"Item 3 Description", @"description", 
                                  nil];

    NSMutableArray* array = [NSMutableArray arrayWithObjects: item1, item2, item3, nil];
    self.popupValues.content = array;
}

@end

然后对于绑定,我将其设置如下:

NSComboBox:

  • Content -> Array Controller.arrangedObjects
  • 内容值 ->数组Controller.arrangedObjects.name
  • 值-> App Delegate.comboBoxValue(如果您希望在 NSComboBox 中输入时逐个字母地更新 NSTextField,请选中连续更新值

NSTextField:

  • Value -> App Delegate.comboBoxValue(如果您希望在 NSTextField 中输入时逐个字母更新 NSComboBox,请选中连续更新值

如果您希望将输入的新值添加到我很遗憾地说,仅使用这两个控件和绑定并不容易实现该数组。这有点复杂。但简单情况的技巧是,除了用于向 NSComboBox 提供预加载值的 NSArrayController 之外,您还需要其他位置来存储值。

The trick here is that you need somewhere else to put the value of the NSComboBox. The NSArrayController is fine for providing the stock values to the NSComboBox, but you can type arbitrary values into an NSComboBox that might not be in the NSArrayController's contentArray, so it's not surprising that you need somewhere else to put the value. I was able to mock this up quickly by just putting a simple value on the AppDelegate like this:

@interface SOAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
// The NSArrayController you were talking about...
@property (assign) IBOutlet NSArrayController* popupValues;    
// The other place to store data...
@property (retain) id comboBoxValue;

@end

Then in the implementation:

@implementation SOAppDelegate

@synthesize window = _window;
@synthesize comboBoxValue = _comboBoxValue;

- (void)dealloc
{
    [_comboBoxValue release];
    _comboBoxValue = nil;
    [super dealloc];
}

-(void)awakeFromNib
{    
    [super awakeFromNib];
    NSMutableDictionary* item1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                                  [NSNumber numberWithUnsignedInteger: 1], @"id", 
                                  @"Item 1 Name", @"name", 
                                  @"Item 1 Description", @"description", 
                                  nil];
    NSMutableDictionary* item2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                                  [NSNumber numberWithUnsignedInteger: 2], @"id", 
                                  @"Item 2 Name", @"name", 
                                  @"Item 2 Description", @"description", 
                                  nil];
    NSMutableDictionary* item3 = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                                  [NSNumber numberWithUnsignedInteger:3], @"id", 
                                  @"Item 3 Name", @"name", 
                                  @"Item 3 Description", @"description", 
                                  nil];

    NSMutableArray* array = [NSMutableArray arrayWithObjects: item1, item2, item3, nil];
    self.popupValues.content = array;
}

@end

Then for the bindings, I set it up like this:

NSComboBox:

  • Content -> Array Controller.arrangedObjects
  • Content Values -> Array Controller.arrangedObjects.name
  • Value -> App Delegate.comboBoxValue (check Continuously Updates Value if you want the NSTextField to be updated letter-by-letter as you're typing in the NSComboBox)

NSTextField:

  • Value -> App Delegate.comboBoxValue (check Continuously Updates Value if you want the NSComboBox to be updated letter-by-letter as you're typing in the NSTextField)

If you want new values you type in to be added to the array, I'm sorry to say, that's not readily doable with just these two controls and bindings. That's a fair bit more complicated. But the trick for the simple case is that you need some place to store the value other than the NSArrayController you're using to provide pre-loaded values to the NSComboBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文