1
votes

I need to perform an operation on all objects in my NSOutlineView when I close the window.

(Both parents and children, and children of children). It doesn't matter if the items are expanded or not, I just need to perform a selector over all items in the outline view.

thanks

1

1 Answers

3
votes

Assuming you're using NSOutlineViewDataSource and not bindings, you could do it like this:

- (void)iterateItemsInOutlineView: (NSOutlineView*)outlineView
{
    id<NSOutlineViewDataSource> dataSource = outlineView.dataSource;
    NSMutableArray* stack = [NSMutableArray array];
    do
    {
        // Pop an item off the stack
        id currentItem = stack.lastObject;
        if (stack.count) 
            [stack removeLastObject];

        // Push the children onto the stack
        const NSUInteger childCount = [dataSource outlineView: outlineView numberOfChildrenOfItem: currentItem];
        for (NSUInteger i = 0; i < childCount; ++i) 
            [stack addObject: [dataSource  outlineView: outlineView child: i ofItem: currentItem]];

        // Visit the current item.
        if (nil != currentItem)
        {
            // Do whatever you want to do to each item here...
        }        
    } while (stack.count);
}

This should achieve a full traversal of all the objects vended by your NSOutlineViewDataSource.

FYI: If you're using cocoa bindings, this won't work as is. If that's the case, though, you could use a similar approach (i.e. surrogate-stack traversal) against the NSTreeController (or whatever else) you're binding to.

HTH