将 NSTableView 绑定到 NSMutableArray
我正在学习 Cocoa,我遇到了一个问题:我想通过绑定将 NSMutableArray 的内容绑定到 NSTableView。我阅读了许多有关它们的文档,但我无法使它们工作(我的表格中没有显示任何内容)。
事实如下:
我创建了一个名为 MTMTask
的简单模型,其中包含 2 个属性,priority
和 text
:
MTMTask.h
@interface MTMTask : NSObject {
NSString *priority;
NSString *text;
}
@property(copy) NSString* priority;
@property(copy) NSString* text;
- (id) initWithPriority :(NSString*)newPriority andText:(NSString*)newText;
@end
MTMTask.m
#import "MTMTask.h"
@implementation MTMTask
@synthesize text, priority;
- (id) initWithPriority:(NSString *)newPriority andText:(NSString *)newText {
if (self = [super init]) {
priority = newPriority;
text = newText;
return self;
}
return nil;
}
@end
然后我创建了 MTMTaskController :
MTMTaskController.h
#import <Cocoa/Cocoa.h>
#import "MTMTask.h"
@interface MTMTaskController : NSObject {
NSMutableArray *_tasksList;
}
- (NSMutableArray *) tasksList;
@end
MTMTaskController.m
#import "MTMTaskController.h"
@implementation MTMTaskController
- (void) awakeFromNib
{
MTMTask *task1 = [[MTMTask alloc] initWithPriority:@"high" andText:@"Feed the hungry cat"];
MTMTask *task2 = [[MTMTask alloc] initWithPriority:@"low" andText:@"Visit my family"];
_tasksList = [[NSMutableArray alloc] initWithObjects:task1, task2, nil];
}
- (NSMutableArray*) tasksList
{
return _tasksList;
}
@end
最后编辑了 MainMenu.xib:我添加了一个 NSObject
并将其类设置为 MTMTaskController
。然后我添加了一个名为 TasksListController
的 NSArrayController,其内容出口绑定到 MMTTaskController.tasksList
。我还将其模式设置为 Class
和类名称 MMTTask
。我将 NSTableView
两列的 value
出口绑定到 TasksListController
文本和优先级。
但是当我运行该程序时,它并没有真正成功:表中没有显示任何内容。
您对我的问题有什么想法吗?我想我错过了一些东西,但我不知道是什么。
提前致谢!
I'm learning Cocoa, and I've got a problem: I would like to bind the content of an NSMutableArray to an NSTableView, with bindings. I read many documentations about them, but I can't manage to make them work (nothing is displayed in my table).
Here are the facts:
I created a simple model, called MTMTask
which contains 2 properties, priority
and text
:
MTMTask.h
@interface MTMTask : NSObject {
NSString *priority;
NSString *text;
}
@property(copy) NSString* priority;
@property(copy) NSString* text;
- (id) initWithPriority :(NSString*)newPriority andText:(NSString*)newText;
@end
MTMTask.m
#import "MTMTask.h"
@implementation MTMTask
@synthesize text, priority;
- (id) initWithPriority:(NSString *)newPriority andText:(NSString *)newText {
if (self = [super init]) {
priority = newPriority;
text = newText;
return self;
}
return nil;
}
@end
Then I've created the MTMTaskController :
MTMTaskController.h
#import <Cocoa/Cocoa.h>
#import "MTMTask.h"
@interface MTMTaskController : NSObject {
NSMutableArray *_tasksList;
}
- (NSMutableArray *) tasksList;
@end
MTMTaskController.m
#import "MTMTaskController.h"
@implementation MTMTaskController
- (void) awakeFromNib
{
MTMTask *task1 = [[MTMTask alloc] initWithPriority:@"high" andText:@"Feed the hungry cat"];
MTMTask *task2 = [[MTMTask alloc] initWithPriority:@"low" andText:@"Visit my family"];
_tasksList = [[NSMutableArray alloc] initWithObjects:task1, task2, nil];
}
- (NSMutableArray*) tasksList
{
return _tasksList;
}
@end
And finally I edited the MainMenu.xib: I added a NSObject
and set its class to MTMTaskController
. Then I added an NSArrayController, called TasksListController
, with its content outlet bound to MTMTaskController.tasksList
. I also set its mode to Class
and class name MTMTask
. I bound the value
outlet of two columns of an NSTableView
to TasksListController
text and priority.
But when I run the program, well, it's not really a success: nothing shows up in the table.
Have you got an idea concerning my problem? I think I'm missing something but I can't figure what.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您从 nib 唤醒时为控制器分配对象时,您将创建对象,将它们添加到数组中,然后将该数组设置为任务列表。
关于绑定的问题是,您需要了解 KVO(键值观察),这是绑定对象知道它们绑定的事物已更改的机制。
在 awake from nib 方法中,您直接设置了数组,这不会调用 KVO。
我创建了一个示例 Xcode 项目 (Xcode 3.1),您可以从此处下载。这会为任务列表创建一个属性,并且在 awakeFromNib 方法中,我使用属性语法分配数组,该语法会为您处理 KVO:
}
或者,您可以将分配夹在
willChangeValueForKey:
和 < code>didChangeValueForKey: 消息,但我将其作为练习留给您。When you are allocating objects for the controller in awake from nib, you create the objects, add them to an array and then set that array as the task list.
The thing about bindings is that you need to be aware of KVO (Key value observing) which is the mechanism by which bound objects know that the things they have bound to have changed.
In the awake from nib method you have just set the array directly which doesn't invoke KVO.
I have created an example Xcode project (Xcode 3.1) which you can download from here. This creates a property for the task list and within the awakeFromNib method I am assigning the array using property syntax, which takes care of KVO for you:
}
Alternatively, you could sandwich the assignment in
willChangeValueForKey:
anddidChangeValueForKey:
messages, but I'll leave that as an excercise for you.