开发者

Getting around IBActions limited scope

I have an NSCollectionView and the view is an NSBox wit开发者_JAVA技巧h 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.


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜