views:

44

answers:

1

Hello,

I have an NSCollectionView and the view is an NSBox with a label and an NSButton. I want a double click or a click of the NSButton to tell the controller to perform an action with the represented object of the NSCollectionViewItem. The Item View is has been subclassed, the code is as follows:

#import <Cocoa/Cocoa.h>
#import "WizardItem.h"

@interface WizardItemView : NSBox {
    id delegate;
    IBOutlet NSCollectionViewItem * viewItem;
    WizardItem * wizardItem;
}

@property(readwrite,retain) WizardItem * wizardItem;
@property(readwrite,retain) id delegate;

-(IBAction)start:(id)sender;

@end

#import "WizardItemView.h"

@implementation WizardItemView
@synthesize wizardItem, delegate;

-(void)awakeFromNib {   
    [self bind:@"wizardItem" toObject:viewItem withKeyPath:@"representedObject" options:nil];
}

-(void)mouseDown:(NSEvent *)event {
    [super mouseDown:event];
    if([event clickCount] > 1) {
        [delegate performAction:[wizardItem action]];
    }   
}

-(IBAction)start:(id)sender {
    [delegate performAction:[wizardItem action]];
}


@end

The problem I've run into is that as an IBAction, the only things in the scope of -start are the things that have been bound in IB, so delegate and viewItem. This means that I cannot get at the represented object to send it to the delegate.

Is there a way around this limited scope or a better way or getting hold of the represented object?

Thanks.

A: 

Firstly, you almost never need to subclass views.

Bind doesn't do what you think - you want addObserver:forKeyPath:options:context: (You should try to understand what -bind is for tho ).

When you say "the key seems to be it being the "prototype" view for an NSCollectionViewItem" I think you are really confused…

Forget IBOutlet & IBAction - they don't mean anything if you are not Interface Builder. "Prototype" means nothing in Objective-c.

The two methods in the view do not have different scope in any way - there is no difference between them at all. They are both methods, equivalent in every way apart from their names (and of course the code they contain).

If wizardItem is null in -start but has a value in -mouseDown this is wholly to do with the timing that they are called. You either have an object that is going away too soon or isn't yet created at a point you think it is.

Are you familiar with NSZombie? You will find it very useful.

mustISignUp
My (newly formed) understanding of it is this: In an NSCollectionView you have an itemPrototype (Cocoa's word, not mine) of class NSCollectionViewItem. Any bindings done in IB are correctly available in the itemPrototype but NOT in the copies of the itemPrototype made for each item of content, so yes, scope is totally the wrong word, they are different instances entirely. What I've got now is a subclass of NSCollectionViewItem as the controller and this can see the representedObject perfectly for button presses, etc. However it cannot see the delegate binding. Joy...
Septih