Graphics Reference
In-Depth Information
input and needs to determine how to process that input. Before diving into the meat of
figuring out how to receive events, we first need to create a custom
NSView
that receives
mouse events and hands them off to a delegate object, as shown in Listing 11-1.
LISTING 11-1
LZContentView Header for Receiving Mouse Events
#import
<Cocoa/Cocoa.h>
@interface
LZContentView : NSView
{
IBOutlet id
delegate
;
}
@end
The header subclasses
NSView
adds one instance variable (or ivar) to the object; the
dele-
gate
. Because this
delegate
is assigned in Interface Builder, it is flagged as an
IBOutlet
.
Whenever we want to bind an object in Interface Builder that is not defined as an
id
, we
need to declare it as an
IBOutlet
to let Interface Builder know it should be exposed.
We want to capture only the
-mouseDown:
and
-mouseUp:
events in the
NSView
subclass,
as shown in Listing 11-2. When captured, those events are sent to the
delegate
, which
handles all other interactions.
LISTING 11-2
LZContentView Implementation File for Receiving Mouse Events
#import
“LZContentView.h”
@implementation
LZContentView
- (
void
)awakeFromNib
{
}
- (
void
)mouseDown:(
NSEvent
*)theEvent
{
[
delegate mouseDown
:theEvent];
}
- (
void
)mouseUp:(
NSEvent
*)theEvent
{
[
delegate mouseUp
:theEvent];
}
@end