Re: Subclass of NSView not responding to mouseDown event
Re: Subclass of NSView not responding to mouseDown event
- Subject: Re: Subclass of NSView not responding to mouseDown event
- From: Paul Buxton <email@hidden>
- Date: Sat, 26 Sep 2009 09:12:53 +0100
Mike,
Thanks that sorted the problem!
I had used the debugger and was hitting breakpoints in the handlers for
setcolor and setradius fine but was not hitting my breakpoint in the
mouseDown handler.
I guess the incorrect message usage was screwing things up in some way. At
least I now know that if I get warnings like that again it is something to
double check quite carefully.
Paul
2009/9/25 Mike Abdullah <email@hidden>
>
> On 25 Sep 2009, at 15:03, Paul Buxton wrote:
>
> 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.
>>
>
> What makes you say that? Have you tested it in the debugger?
>
>>
>> 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?
>>
>
> That's because there is no -needsDrawing: or -needsDisplay: method. There
> is however, -setNeedsDisplay: which is almost certainly what you want.
>
>>
>> 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.
>>
>> You generally don't want -acceptsFirstMouse. Read up on what that method
> does. Either way, neither of these should have an effect on whether your
> class receives a -mouseDown: event.
>
>>
>> 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
>>
>
>
_______________________________________________
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