Subclass of NSView not responding to mouseDown event
Subclass of NSView not responding to mouseDown event
- Subject: Subclass of NSView not responding to mouseDown event
- From: Paul Buxton <email@hidden>
- Date: Fri, 25 Sep 2009 15:03:58 +0100
Hi guys. I am working though the book 'Learning Cocoa with Objective-C 2nd
edition and have reached the Dot View example. It is a simple app that
should demonstrate responding to mouse events. Apart from having to cope
with the difference between the version of X-Code the book was developed
with, and the more up to date version I am using, I haven't had any trouble
with the book until now. I created a class DotView which is a subclass of
NSView.
I Added a Custom view to my app and set it's class to be DotView.
In the DotView class I have implemented the mouseDown event handler, but I
do not seem to be getting the events.
I do get some build warnings that the DotView class may not respond to the
needsDrawing message, and when typing the code the autocomplete doesn't
suggest mouseDown as an option.
It seems as though it hasn't really made it a subclass of NSView, any
thoughts?
Looking through the lists I saw some suggestions to previous questions that
suggested adding the acceptsFirstMouse and acceptsFirstResponder methods, so
I have added those as well, but still no luck.
Sample code below.
//
// DotView.h
//
// Created by Paul Buxton on 25/09/2009.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface DotView : NSView {
IBOutlet NSColorWell *colorWell;
IBOutlet NSSlider *slider;
NSPoint center;
NSColor *color;
float radius;
}
- (IBAction)setColor:(id)sender;
- (IBAction)setRadius:(id)sender;
@end
//
// DotView.m
//
// Created by Paul Buxton on 25/09/2009.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "DotView.h"
@implementation DotView
-(id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
center.x=50.0;
center.y=50.0;
radius=20;
color = [[NSColor redColor] retain];
return self;
}
-(void)awakeFromNib
{
[colorWell setColor:color];
[slider setFloatValue:radius];
}
-(void)dealloc
{
[color release];
[super dealloc];
}
-(void)drawRect:(NSRect)dirtyRect
{
NSRect dotRect;
[[NSColor whiteColor] set];
NSRectFill([self bounds]);
dotRect.origin.x = center.x - radius;
dotRect.origin.y = center.y - radius;
dotRect.size.width = 2*radius;
dotRect.size.height = 2*radius;
[color set];
[[NSBezierPath bezierPathWithOvalInRect:dotRect] fill];
}
-(BOOL)isOpaque
{
return YES;
}
-(void)mouseDown:(NSEvent *)event
{
NSPoint eventlocation = [event locationInWindow];
center = [self convertPoint:eventlocation fromView:nil];
[self needsDisplay:YES];
}
- (IBAction)setColor:(id)sender {
NSColor *newColor = [sender color];
[newColor retain];
[color release];
color = newColor;
[self needsDisplay:YES];
}
- (IBAction)setRadius:(id)sender {
radius = [sender floatValue];
[self needsDisplay:YES];
}
-(BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
return YES;
}
-(BOOL)acceptsFirstResponder
{
return YES;
}
@end
Cheers,
Paul
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden