Re: NSView subclass
Re: NSView subclass
- Subject: Re: NSView subclass
- From: Jonathan Hess <email@hidden>
- Date: Wed, 15 Oct 2008 14:46:49 -0700
On Oct 15, 2008, at 2:30 PM, Scott Andrew wrote:
If its a delegate you would want to check if the delegate handles
the selector with respondsToSelector and the use performSelector to
make the call. For example....
if ([delegate respondsToSelector:@selector(pointClicked:)])
[delegate performSelector:@selector(pointClicked:) withObject:
[NSValue valueWithPoint:(pt)]];
NSValue is a normal class just like any other class. KVO, and KVC
happen to use NSValue to box and unbox non object types. If you pass a
function or method an NSValue, it will receive an NSValue. Methods
like setValue:forKey: actually receive an NSValue object, but then
manually unbox the value before calling the methods indicated by the
second key argument.
Since you've declared your delegate method in a header, you can call
it directly. This eliminates the need for performSelector:withObject:,
which is probably what caused Scott to try to use NSValue.
You can now write:
if ([delegate respondsToSelector:@selector(pointClicked:)]) {
[delegate pointClicked:pt];
}
Also, instead of this:
@interface NSObject ()
- (void)pointClicked:(NSPoint)point;
@end
You should write
@interface NSObject (MyViewSubclassDelegation)
- (void)pointClicked:(NSPoint)point;
@end
Having the category name is more descriptive. Also, having an empty
category name is a special form and means you're using a class
extension. You can read about those here: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_4_section_5.html#/
/apple_ref/doc/uid/TP30001163-CH20-SW2
Good Luck -
Jon Hess
This should work. I believe the NSValue will resolve to the NSPoint
in runtime.
Scott Andrew
On Oct 15, 2008, at 2:15 PM, DKJ wrote:
I've written a subclass of NSView. It calls a method its delegate
can implement to detect mouse clicks. I've put something like this
in the header file:
// delegate method:
@interface NSObject ()
- (void)pointClicked:(NSPoint)point;
@end
This is enough to prevent a "no -pointClicked: method found"
compiler warning. But is it the best way to do it?
dkj
_______________________________________________
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
_______________________________________________
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