简体   繁体   English

为什么我的简单可可绑定功能不起作用?

[英]Why doesn't my simple Cocoa binding work?

Cocoa bindings, KVC, and KVO are starting to make my head hurt. 可可胶,KVC和KVO开始伤害我的头部。 All I want to do is have an NSTextField's value bound to the value of a property of my view controller. 我要做的就是将NSTextField的值绑定到我的视图控制器的属性的值。 Could someone tell me where I'm going wrong? 有人可以告诉我我要去哪里了吗? Any help would be greatly appreciated. 任何帮助将不胜感激。 Below is a simplified version of what I've got going on. 以下是我正在做的事情的简化版本。

MyViewController.h: MyViewController.h:

#import <Cocoa/Cocoa.h>

@interface MyViewController : NSViewController

@property NSString *colorSpaceName;
@property IBOutlet NSTextField *colorSpaceLabel;

@end

MyViewController.m: MyViewController.m:

#import "MyViewController.h"

@implementation MyViewController

@synthesize colorSpaceName;

- (id)initWithNibName:(NSString *)nibNameOrNil 
               bundle:(NSBundle *)nibBundleOrNil
{
    // ...
    if ( self ) {
        [self.colorSpaceLabel bind:@"stringValue" 
                          toObject:self 
                       withKeyPath:@"colorSpaceName" 
                           options:nil];
    }
    // ...
}

@end

According to IB there is no 'stringValue" binding for NSTextField just a 'value' binding. Unless you are setting up your UI all in code the easiest thing to do is to use IB for bindings. 根据IB的说法, NSTextField没有“ stringValue”绑定,只有“值”绑定,除非您在代码中全部设置UI,否则最简单的方法就是使用IB进行绑定。

Select the NSTextField in the xib file. 在xib文件中选择NSTextField Then select the bindings tab in the utilities area to the right. 然后,在右侧的实用程序区域中选择绑定选项卡。 The first binding listed should be value , expand it. 列出的第一个绑定应该是value ,将其展开。 From the popup menu select "File's Owner" as the object to bind to. 从弹出菜单中选择“文件的所有者”作为要绑定的对象。 Xcode will enter self into the Model Key Path field for you, just add .colorSpaceName to the end of the field and press return. Xcode将为您self输入“模型关键路径”字段,只需在该字段的末尾添加.colorSpaceName并按回车即可。

If you really must do the binding in code then Change @"stringValue" to @"value" and make sure that your outlet is connected in IB. 如果确实必须在代码中进行绑定,则将@"stringValue"更改为@"value" ,并确保您的插座已连接IB。

Note: If you are creating the UI in code there is no need to declare any of the elements as IBOutlet since that and IBAction are just keywords for IB to know what properties and methods to pay attention to. 注意:如果使用代码创建UI,则无需将任何元素声明为IBOutlet因为那和IBAction只是IB的关键字,IB可以知道要注意哪些属性和方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM