NSObjectController 混淆绑定到类属性。帮助!
我大部分时间都在自学可可并享受这种体验。我一整天都在努力解决一个简单的问题,谷歌让我失望了。我已阅读 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 时我都想更新它更改歌曲的版本。我执行以下操作:
将 NSObjectController 对象拖到文档窗口和我设置的 Inspector->Attributes 中:
- 模式:类
- 类名:MTSong
- 添加一个名为 version 的键和另一个名为 name 的键
Go to Inspector->Bindings-> ;控制器内容
- 绑定到:文件所有者(不确定这是否正确...)
- 模型密钥路径:版本
选择标签的单元格并转到 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 步:
- 转到检查器->绑定->控制器内容
- 绑定到:应用控制器
- 模型密钥路径:mySong
我需要更改 3.
- 选择标签的单元格并转到 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:
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
Go to Inspector->Bindings->Controller Content
- Bind To: File's Owner (Not sure this is right...)
- Model Key Path: version
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:
- Go to Inspector->Bindings->Controller Content
- Bind To: App Controller
- Model Key Path: mySong
I needed to change 3. to
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您希望按照您的建议将控制器的内容绑定到 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).