开发者

NSOutlineView outlineViewSelectionDidChange

my NSOutlineView outlineViewSelectionDidChange method will not be called. I set set the NSOutlineViews delegate to the class where the other methods such as

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item

exist. But outlineViewSelectionD开发者_StackOverflow中文版idChange will not be called on selecting an item. Does anybody has an idea?


This notification is a bit odd, in that it is not automatically forwarded to delegates. Try adding an explicit registration to your initialization code, like this example:

- (void)windowControllerDidLoadNib:(NSWindowController *)aController;
{
    [super windowControllerDidLoadNib:aController];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
               selector:@selector(outlineViewSelectionDidChange:)
                   name:@"NSOutlineViewSelectionDidChangeNotification"
                 object:outlineView];
}


Okay, meanwhile i figured out that the "NSOutlineViewSelectionDidChangeNotification" will be thrown only within the notification object. So i had to subclass my NSOutlineView to catch the notification and pass it to the object where i need it.


Your own view needs to conform to the NSOutlineViewDelegate protocol like so..

@interface MyOutlineViewController : NSView <NSOutlineViewDataSource,NSOutlineViewDelegate> {
    IBOutlet NSOutlineView *myoutlineview;
}
@end

you will have this methods in your implementation

-(NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item;
-(BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item;
-(id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item;
-(id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item;

where you setup your outlineview. When loading this view -(void)viewDidLoad gets called and your predefined nib/xib file or your manual call will set your datasource to fill it depending on your logic.

Now in your -(void)viewDidLoad your myoutlineview needs to set its own delegate with

[myoutlineview setDelegate:self];

so your own View may know where to call its notification methods triggerd from selections and so on. So you can place your notification logic inside the same View class conforming to this protocol.

-(void)outlineViewSelectionDidChange:(NSNotification *)notification {
    NSLog(@"selection did change");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜