NSObjectController 混淆绑定到类属性。帮助!

发布于 2024-08-21 00:50:28 字数 2240 浏览 1 评论 0原文

我大部分时间都在自学可可并享受这种体验。我一整天都在努力解决一个简单的问题,谷歌让我失望了。我已阅读 Cocoa Bindings Program Topics 并认为我理解了它,但仍然无法解决我的问题。

我有一个非常简单的类,名为 MTSong,它具有各种属性。我已经使用 @synthesize 创建 getter/setter,并且可以使用 KVC 来更改属性。即在我的应用程序控制器中,以下工作有效:

mySong = [[MTSong alloc]init];
[mySong setValue:@"2" forKey:@"version"];

如果我在类代码 MTSong.h 中做一些奇怪的事情,则 MTSong.h 是:

#import <Foundation/Foundation.h>

@interface MTSong : NSObject {
    NSNumber    *version;
    NSString    *name;
}
@property(readwrite, assign) NSNumber *version;
@property(readwrite, assign) NSString *name;
@end

并且 MTSong.m 是:

#import "MTSong.h"

@implementation MTSong

- (id)init
{
    [super init];
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

@synthesize version;
@synthesize name;
@end

在 Interface Builder 中,我有一个标签(NSTextField),每当我使用 KVC 时我都想更新它更改歌曲的版本。我执行以下操作:

  1. 将 NSObjectController 对象拖到文档窗口和我设置的 Inspector->Attributes 中:

    • 模式:类
    • 类名:MTSong
    • 添加一个名为 version 的键和另一个名为 name 的键
  2. Go to Inspector->Bindings-> ;控制器内容

    • 绑定到:文件所有者(不确定这是否正确...)
    • 模型密钥路径:版本
  3. 选择标签的单元格并转到 Inspector< /p>

    • 绑定到:对象控制器
    • 控制器键:mySong
    • 模型密钥路径:版本

我尝试将步骤 2 中的模型密钥路径更改为“mySong”,这更有意义,但编译器抱怨。任何建议将不胜感激。

Scott


更新帖子评论

我没有公开 mySong 属性,因此将我的 AppController.h 更改为:

#import <Cocoa/Cocoa.h>
@class MTSong;

@interface AppController : NSObject {
    IBOutlet NSButton *start;
    IBOutlet NSTextField *tf;
    MTSong *mySong;
}
-(IBAction)convertFile:(id)sender;
@end

我怀疑文件的所有者是错误的,因为我没有使用基于文档的应用程序,并且我需要绑定到 AppController,所以现在是第 2 步:

  1. 转到检查器->绑定->控制器内容
    • 绑定到:应用控制器
    • 模型密钥路径:mySong

我需要更改 3.

  1. 选择标签的单元格并转到 Inspector
    • 绑定到:对象控制器
    • 控制器按键:选择
    • 模型密钥路径:版本

全部编译并运行良好!

I'm teaching myself cocoa and enjoying the experience most of the time. I have been struggling all day with a simple problem that google has let me down on. I have read the Cocoa Bindings Program Topics and think I grok it but still can't solve my issue.

I have a very simple class called MTSong that has various properties. I have used @synthesize to create getter/setters and can use KVC to change properties. i.e in my app controller the following works:

mySong = [[MTSong alloc]init];
[mySong setValue:@"2" forKey:@"version"];

In case I am doing something noddy in my class code MTSong.h is:

#import <Foundation/Foundation.h>

@interface MTSong : NSObject {
    NSNumber    *version;
    NSString    *name;
}
@property(readwrite, assign) NSNumber *version;
@property(readwrite, assign) NSString *name;
@end

and MTSong.m is:

#import "MTSong.h"

@implementation MTSong

- (id)init
{
    [super init];
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

@synthesize version;
@synthesize name;
@end

In Interface Builder I have a label (NSTextField) that I want to update whenever I use KVC to change the version of the song. I do the following:

  1. Drag NSObjectController object into the doc window and in the Inspector->Attributes I set:

    • Mode: Class
    • Class Name: MTSong
    • Add a key called version and another called name
  2. Go to Inspector->Bindings->Controller Content

    • Bind To: File's Owner (Not sure this is right...)
    • Model Key Path: version
  3. Select the cell of the label and go to Inspector

    • Bind to: Object Controller
    • Controller Key: mySong
    • Model Key Path: version

I have attempted changing the Model Key Path in step 2 to "mySong" which makes more sense but the compiler complains. Any suggestions would be greatly appreciated.

Scott


Update Post Comments

I wasn't exposing mySong property so have changed my AppController.h to be:

#import <Cocoa/Cocoa.h>
@class MTSong;

@interface AppController : NSObject {
    IBOutlet NSButton *start;
    IBOutlet NSTextField *tf;
    MTSong *mySong;
}
-(IBAction)convertFile:(id)sender;
@end

I suspect File's owner was wrong as I am not using a document based application and I need to bind to the AppController, so step 2 is now:

  1. Go to Inspector->Bindings->Controller Content
    • Bind To: App Controller
    • Model Key Path: mySong

I needed to change 3. to

  1. Select the cell of the label and go to Inspector
    • Bind to: Object Controller
    • Controller Key: selection
    • Model Key Path: version

All compiles and is playing nice!

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

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

发布评论

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

评论(1

从来不烧饼 2024-08-28 00:50:28

您希望按照您的建议将控制器的内容绑定到 mySong 键路径。您可能没有做的是将 mySong 作为文件所有者(通常是您的应用程序委托)中的属性或实例方法公开。

You want to bind the controller's content to the mySong key path as you suggested. What you are perhaps not doing is exposing mySong as a property or instance method in the File's Owner (typically your application delegate).

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