Re: Custom Controls and Controllers: Some funny stuff going on here, any ideas?
Re: Custom Controls and Controllers: Some funny stuff going on here, any ideas?
- Subject: Re: Custom Controls and Controllers: Some funny stuff going on here, any ideas?
- From: Chris Hanson <email@hidden>
- Date: Tue, 29 Oct 2002 21:16:36 -0600
At 10:38 AM +1100 10/30/02, Simon Parkinson-Bates wrote:
I then implemented the mouseXXX methods and tried to get them to forward the
action to DVBitManipulator's target.
I think you're not quite clear on how targets and actions work.
Your DVBitManipulator control shouldn't send a particular action to
its target. Rather, it should send the action specified in Interface
Builder to its target:
[self sendAction:[self action]
toTarget:[self target]];
In Interface Builder, to tell your custom control what its target and
action are, control-drag from your DVBitController instance to your
DVBitManipulator instance. Then, in the Connections pane of the
Inspector, select "target>onBitManipulator:" and click the Connect
button. This should be almots all that you need to do.
FYI, your post has some terminology issues. The proper way to refer
to an instance method in a particular class is -[DVBitManipulator
awakeFromNib] (for example), not using C++-style "::" syntax. Also,
you don't "call methods" in Objective-C, you send messages. (Which
method should be invoked when a message is sent to an object is
determined dynamically, at runtime.)
So I added to the DVBitManipulator subclass the method:
"-(void)setTarget:(id)anObject;"
and a local member:
"IBOutlet NSObject *m_pTarget;"
Cocoa is not MFC, and the Macintosh is not Windows. (Thankfully!)
Objective-C has its own naming conventions, and because Objective-C
has dynamic language features like object introspection it's
important to actually adhere to these naming conventions.
A proper name would be either "target" or "_target".
Not only that, but it's almost always an error to write "NSObject *"
instead of "id". Typically you either want to use an object of a
more specific class than NSObject, or you want to use any class of
object.
So now that you know what going on... the question:
Why is it that even though the Cocoa framework is calling
DVBitManipulator::-(void)setTarget:(id)anObject
is the call
[self target];
returning me NULL! Solution, ideas welcome!
The nib-loading code sends your object setTarget: because it does
this for any attribute it is configuring in any object. Any time the
nib-loading code wants to set the value of the attribute "xxx" on an
object, it checks whether the object responds to setXxx: and if so
sends the object setXxx: instead of setting the value of the
attribute directly.
However, you've only overridden -[NSControl setTarget:]. Unless
you're not telling us something, you haven't overridden -[NSControl
target] to return the value of your _target instance variable.
My guess is that your control doesn't have an associated NSCell. If
you override the NSControl class method +[NSControll cellClass] to
return NSActionCell or a custom subclass, that should be all you need
to do. You shouldn't have to dance around implementing your own
accessors for the "target" attribute.
(Actually, you should read the "Controls and Cells" programming topic
in the Cocoa documentation, and probably do most of the work you're
doing in an NSControl subclass right now in an NS(Action)Cell
subclass instead. Control and cell subclasses are usually paired...)
-- Chris
--
Chris Hanson | Email: email@hidden
bDistributed.com, Inc. | Phone: +1-847-372-3955
Making Business Distributed | Fax: +1-847-589-3738
http://bdistributed.com/ | Personal Email: email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.