Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSControl subclass can't read the target?

The following code:

- (void) setTarget:(id)anObject {   
    NSLog(@"anObject: %@",anObject);
    [super setTarget:anObject];
    NSLog(@"Target: %@",[self target]);
}

has this output:

anObject: <DropZoneViewController: 0x15dd5770>
Target: (null)

This is in a subclass of NSControl. So what am I doing wrong?

What I'm trying to achieve: I have an NScontrol that accepts dragging objects. When the dragging ends, I'd like to send the control's action to its target. But how do I get the control's action & target if this above doesn't work?

like image 285
V1ru8 Avatar asked Oct 08 '10 08:10

V1ru8


1 Answers

NSControl doesn’t store it’s own target, that is what it’s cell is supposed to do.

So there are two reasons this might fail:

  1. Your control doesn’t have a cell
    In this case you really should create a subclass of NSActionCell to implement your control. Your subclass of NSControl shouldn’t do much besides setting up the cell.
    If you don’t want to do it the right way by using a NSCell you’ll have to add instance variables to your NSControl subclass to store the target and action and override the getters and setters to use them.

  2. Your cell is not a subclass of NSActionCell. A regular NSCell doesn’t store a target either.
    If you’re using a custom cell that is not a subclass of NSActionCell just change it so that it does inherit from NSActionCell instead of NSCell. If you cannot do this (for example because you’re subclassing a NSCell subclass you cannot change) you’ll have to add the instance variables for target and selector to your cell class and override it’s setters and getters.

like image 92
Sven Avatar answered Oct 08 '22 18:10

Sven