0
votes

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.

1
Hi, i am struggling to understand your question.. maybe we could rewrite it a little and you may get some answers… Some Questions.. why do you subclass NSBox ? This is the wrong way to go about things in cocoa or MVC in general. This code should be in a windowController or viewController. What is -start: ? You don't mention. Also, i could be wrong, but i dont think bind: toObject: withKeyPath: does what you think it does.hooleyhoop
While i'm at it.. An IBAction isn't really 'a thing'. IBAction is just a label for Interface Builder. A method labelled IBAction is just a regular method. It doesn't in any way 'have limited scope'. I find any talk of 'scope' in objective-c probably indicates some confusion coming from a different language. It is rarely a useful thing to talk about in objective-c.hooleyhoop
"confusion coming from a different language" - undoubtedly ;). I think scope was a fairly decent way of describing it, but oh well. I'm aware IBAction is just for IB, but the key seems to be it being the "prototype" view for an NSCollectionViewItem. The two methods in the view seem to have different scope. In mouseDown wizardItem has a value, in start, it is null.Septih
In regards to your first questions, I struggled with the documentation for NSCollectionView and this obviously messy way is how I've managed to get things working. If you could point me in the direction of the proper way to deal with actions in the views of items in an NSCollectionView I'd be very grateful. bind: toObject: withKeyPath: was the way I found to grab the representedObject to deal with on an individual basis (e.g. menus with different options based on the represented object). Again this wasn't clear to find.Septih

1 Answers

0
votes

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.