开发者

NSTreeController add and addChild methods

GOTO UPDATE

I have a NSTreeController controller that binded (Content Object) to content (NSTreeNode *) of my application delegate. Also I have NSOutlineView in my window that binded to my controller.

Everything works fine, the content is displayed in outline view, but I can not add (addChild, insert etc) any items though controller methods and canAdd (canAddChild etc) methods of controller always return NO (I have a buttons which "enabled" is binded to controller and this buttons are disabled).

Where is my mistake?

UPDATE

I want to give some more information about my situation.

I have a MyNode class:

@interface MyNode : NSTreeNode {
    NSString* title;
}

@property (retain) NSString* title;


@end

I also have the NSTreeController object in my nib file. The childrenKeyPath is set to childNodes in IB and the countKeyPath and isLeafKeyPath are empty.

The controller mode is set to Class and Class name is MyNode.

The Content Object (not Content Array) is binded to MyDocuments rootNode property:

@property (readonly) MyNode* rootNode;

...

rootNode = [[MyNode alloc] init];
rootNode.title = @"Root";

MyNode *childNode = [[MyNode alloc] init];
childNode.title = @"Child";

[[rootNode mutableChildNodes] addObject:childNode];

And I have NSOulineView in my window which content is binded to TreeController's arrangedObjects. The content is displayed. I even can edit the title of my nodes, but I can not add or insert any (child) nodes through my TreeController.

I have 2 buttons: "Add Child" and "Insert Child" connected to the addChild and insertChild actions of TreeCont开发者_运维知识库roller and enabled is binded to canAddChild and canInsertChild properties of TreeController. The buttons are disabled.. and I cannot add or insert any child nodes.

Where is mistake?

UPDATE 2

I have some good news =)

I changed my Document class like this:

@interface MyDocument : NSDocument {
    NSMutableArray *rootNodes;
    MyNode* rootNode;
}

@property (readonly) NSArray* rootNodes;

..

rootNodes = [[NSMutableArray alloc] init];

rootNode = [[MyNode alloc] init];
rootNode.title = @"Root";

MyNode* childNode = [[MyNode alloc] init];
childNode.title = @"Child";

[[rootNode mutableChildNodes] addObject:childNode];

[rootNodes addObject:rootNode];

And binded Content Array (not Content Object) to MyDocument.rootNodes property and everything works fine.

Why doesn't the addXXX methods work with single object? I can not find anything about it in documentation...

And why can TreeController add root elements to the rootNodes? rootNodes is the NSArray, not the NSMutableArray.


I think I've got it! Try setting childrenKeyPath to mutableChildNodes.


You should not add nodes by writing

[rootNodes addObject:rootNode];

Instead you should have this in File's Owner of your XIB/NIB file (MyDocument class):

IBOutlet NSTreeController* treeController;

And connect this Outlet in IB with your Tree Controller. Then just write:

//[rootNodes addObject:rootNode];
  [treeController addObject:rootNode];

By doing so, you also can get rid of rootNodes init:

//rootNodes = [[NSMutableArray alloc] init];

Also, to add child nodes, you can use

[treeController addChild:childNode];

This will add a childNode to the currently selected node.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜