1

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:

  • Declare instance variables for clicked co-ordinates in the custom view.
  • Implement an (IBAction) in the controller class:
  • Use the (id)sender pointer to access the instance variables that hold the co-ordinates
  • Link the IBAction to the custom view in interface builder

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!

tom1990
  • 602
  • 8
  • 17

1 Answers1

4

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];
}
Taum
  • 2,511
  • 18
  • 18
  • Looks like i'm going to have to do a little reading up on this... I have not really gotten my head around the delegate pattern yet, having not encountered a real need for it up until now I suppose (or perhaps I have been avoiding it and finding other ways). – tom1990 Nov 29 '12 at 20:36
  • After a little reading, I can see that this is clearly the right way to go about things! Thanks for pointing this out, I am quite new to object-oriented programming and this is the first time I have come across something in my own programming that needed me to use this method, so it did not occur to me! – tom1990 Nov 30 '12 at 16:53