objective-coopmodel-view-controlleribactiontarget-action

Can I implement a custom target-action in a NSView subclass?


I currently have a custom view class which is drawing a 2d game board representation on a window.

When the user clicks on the board I am using the mouseDown: event to calculate the cell co-ordinates from the mouse position (I am doing this within the custom view class).

- (void)mouseDown:(NSEvent*)theEvent {

    // Get position of mouse click:
    NSPoint point = [theEvent locationInWindow];
    NSPoint mousePos = [self convertPoint:point fromView:nil];

    // Calculate which cell has been clicked:
    int cellX = mousePos.x / gridSize.width;
    int cellY = mousePos.y / gridSize.height;

}

(In the above snippet 'gridSize' is an NSPoint instance variable that I am using to store the height and with of each cell in the game board)

Is it possible for me to create an IBAction style message in my controller class that can be used to send this information?

The way I was imagining this could work was:

To try this I declared 'cellX' and 'cellY' as instance variables in my custom view. I then implemented the following in my controller class:

-(IBAction)cellClicked:(id)sender { 

    [self setCellAtPosX:[sender cellX] PosY:[sender cellY];
}

(cellX and cellY are accessor methods to the instance variables)

As I was expecting it is not that simple, and it will not let me link the IBAction to the custom view. Is there anything missing from my approach to implementing this?

I can imagine that all this would need is some kind of notifier that tells interface builder where to send out a target-action message - in my case, at the end of the mouseDown: implementation, but I cannot find where to begin with this!


Solution

  • You may have less trouble implementing this with a delegate pattern, i.e. set a IBOutlet delegate property on your board view and have the controller implement this protocol. In Interface Builder you will link your view and controller via the outlet, and the view will call it's delegate method in the mouseDown implementation, for instance:

    - (void)mouseDown:(NSEvent*)theEvent {
       ...
       [self.delegate boardView:self didSelectCellAtX:x Y:y];
    }