如何使用Cocoa Bindings实现基于视图的源列表(NSOutlineView)的示例? [英] Example of how to implement a view-based source list (NSOutlineView) using Cocoa Bindings?

查看:222
本文介绍了如何使用Cocoa Bindings实现基于视图的源列表(NSOutlineView)的示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人找到一个清晰,简洁的例子或指南如何使用在Lion中引入的基于视图的NSOutlineView实现源列表?我看过苹果的示例项目,但没有任何方向或解释的感觉,我发现很难掌握它们的工作原理的概念。

Has anybody found a clear, concise example or guide on how to implement a source list using the view-based NSOutlineView introduced in Lion? I've looked at Apple's example project, but without any sense of direction or explanation, I'm finding it difficult to grasp the concept of exactly how they work.

我知道如何使用优秀的PXSourceList作为后备,但真的想开始使用基于视图的源列表,而不是如果可能的话。

I know how to use the excellent PXSourceList as a fallback, but would really like to start using view-based source lists instead if at all possible.

推荐答案

您使用cocoa-bindings标记,因此我假设您的意思是与绑定。我打了个快速的例子。从Xcode中的一个新的非基于文档的Cocoa应用程序模板开始。无论你喜欢什么。首先我添加了一些代码,使一些假数据绑定。这是我的AppDelegate标题看起来像:

You tagged this with the cocoa-bindings tag, so I assume you mean "with bindings." I whipped up a quick example. Start from a new non-document-based Cocoa Application template in Xcode. Call it whatever you like. First I added some code to make some fake data to bind to. Here's what my AppDelegate header looks like:

#import <Cocoa/Cocoa.h>

@interface SOAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (retain) id dataModel;

@end

这里是我的AppDelegate实现: p>

And here's what my AppDelegate implementation looks like:

#import "SOAppDelegate.h"

@implementation SOAppDelegate

@synthesize window = _window;
@synthesize dataModel = _dataModel;

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

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application

    // Make some fake data for our source list.
    NSMutableDictionary* item1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 1", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2_1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.1", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2_2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.2", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2_2_1 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.2.1", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item2_2_2 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 2.2.2", @"itemName", [NSMutableArray array], @"children", nil];
    NSMutableDictionary* item3 = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"Item 3", @"itemName", [NSMutableArray array], @"children", nil];

    [[item2_2 objectForKey: @"children"] addObject: item2_2_1];
    [[item2_2 objectForKey: @"children"] addObject: item2_2_2];

    [[item2 objectForKey: @"children"] addObject: item2_1];
    [[item2 objectForKey: @"children"] addObject: item2_2];

    NSMutableArray* dataModel = [NSMutableArray array];

    [dataModel addObject: item1];
    [dataModel addObject: item2];
    [dataModel addObject: item3];

    self.dataModel = dataModel;
}

@end

我创建的假数据结构,我只想显示几个子级别的东西,等等。唯一重要的是你在Interface Builder中的绑定中指定的关键路径与数据中的键一致(假的

There's no particular significance to the fake data structure I created, I just wanted to show something with a couple of sub-levels, etc. The only thing that matters is that the key paths you specify in the bindings in Interface Builder line up with the keys in your data (fake data in this case.)

然后选择 MainMenu.xib 文件。在IB编辑器中,执行以下步骤:

Then select the MainMenu.xib file. In the IB editor, do the following steps:


  1. 使用对象库(Ctrl-Cmd-Opt-3)添加NSTreeController您的 .xib

  2. 选择NSTreeController,并使用属性检查器(Cmd-Opt-4) > c> c>( c <>);
  3. 在仍选择NSTreeController的情况下,使用绑定检查器(Cmd-Opt-7)将内容数组绑定到AppDelegate,模型键路径为 dataModel

  4. 接下来使用对象库(Ctrl-Cmd-Opt-3)将NSOutlineView添加到您的 .xib

  5. 在窗口内安排它令人满意(通常是窗口的整个高度,选择NSOutlineView(注意,当你第一次点击它,你可能选择了包含它的NSScrollView。第二次单击它,你将会深入到NSOutlineView本身。请注意,如果您扩大IB编辑器左侧所有对象的区域,这将 MUCH 更容易 - 这允许您将对象看作树,并以这种方式导航和选择它们。 )

  6. 使用属性检查器(Cmd-Opt-4)设置NSOutlineView:


    • View Based

    • 1

    • 突出显示源列表

  1. Use the Object Library (Ctrl-Cmd-Opt-3) to add an NSTreeController to your .xib.
  2. Select the NSTreeController, and using the Attributes Inspector (Cmd-Opt-4) set Key Paths > Children to children (for this example; For your data, this should be whatever returns the array of child objects.)
  3. With the NSTreeController still selected, use the Bindings Inspector (Cmd-Opt-7) to bind the Content Array to the AppDelegate, with a Model Key Path of dataModel
  4. Next use the Object Library (Ctrl-Cmd-Opt-3) to add an NSOutlineView to your .xib.
  5. Arrange it to your satisfaction inside the window (typically the entire height of the window, flush against the left-hand side)
  6. Select the NSOutlineView (note that the first time you click on it, you have likely selected the NSScrollView that contains it. Click on it a second time and you'll have drilled-down to the NSOutlineView itself. Note that this is MUCH easier if you widen the area on the left of the IB editor where all the objects are -- this allows you see the objects as a tree, and navigate and select them that way.)
  7. Using the Attributes Inspector (Cmd-Opt-4) set the NSOutlineView:
    • Content Mode: View Based
    • Columns: 1
    • Highlight: Source List

保存。跑。您应该看到一个源列表,一旦你扩展了带有子元素的节点,你可能会看到这样的:

Save. Run. You should see a source list, and once you've expanded the nodes with children, you might see something like this:

如果您在Apple开发者计划中,您应该能够请访问 WWDC 2011视频。有一个专门致力于使用基于视图的NSTableView(和NSOutlineView),它包括非常详细的绑定覆盖。

If you're in the Apple Developer Program, you should be able to access the WWDC 2011 Videos. There's one specifically dedicated to working with View-based NSTableView (and NSOutlineView) and it includes pretty thorough coverage of bindings.

希望有帮助!

这篇关于如何使用Cocoa Bindings实现基于视图的源列表(NSOutlineView)的示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆