Custom Controls and Controllers: Some funny stuff going on here, any ideas?
Custom Controls and Controllers: Some funny stuff going on here, any ideas?
- Subject: Custom Controls and Controllers: Some funny stuff going on here, any ideas?
- From: Simon Parkinson-Bates <email@hidden>
- Date: Wed, 30 Oct 2002 10:38:03 +1100
First off, here is the situation:
I firstly created a custom control in IB and generated the following class
(note: a lot of stuff removed for clarity)
@interface DVBitManipulator : NSControl
{
// .. some member variables.
}
// various methods...
-(void)mouseDragged:(NSEvent*)theEvent;
-(void)mouseDown:(NSEvent *)theEvent;
#end
As you can see I implemented the mouseDragged and mouseDown methods.
I've also created for myself a controller class which has been
auto-generated for me by IB (note: a lot of stuff removed for clarity).
@interface DVBitController : NSObject
{
IBOutlet DVBitManipulator *bitManipulator;
}
- (IBAction)onBitManipulator:(id)sender;
end;
As you can see I've created both an outlet and an action in this controller,
(Ctrl dragged from my custom control "DVBitManipulator" to an instance of
"DVBitmanipulator" and visa versa).
I then implemented the mouseXXX methods and tried to get them to forward the
action to DVBitManipulator's target.
-(void)mouseDragged:(NSEvent*)theEvent
{
// doing some stuff....
// forward the action to current target.
[self sendAction:@selector(onBitManipulator:) to:[self target]];
}
-(void)mouseDown:(NSEvent *)theEvent
{
// doing some stuff....
// forward the action to current target.
[self sendAction:@selector(onBitManipulator:) to:[self target]];
}
But when I tried to do this I found that the value of [self target] was
NULL!
So I thought that obviously the target was not being set up, so I added to
the DVBitController subclass a "-(void)awakeFromNib;" method and implemented
it as such...
-(void)awakeFromNib
{
[bitManipulator setTarget:self];
}
thinking that this would resolve the problem... It didn't the value of
"[self target]" was still NULL;
So I added to the DVBitManipulator subclass the method:
"-(void)setTarget:(id)anObject;"
and a local member:
"IBOutlet NSObject *m_pTarget;"
and changed the calls in the mouseXXX methods from
[self sendAction:@selector(onBitManipulator:) to:[self target]];
to
[self sendAction:@selector(onBitManipulator:) to:m_pTarget];
I then ran the debugger and low and behold, it seems that the Cocoa
framework was actually calling my method
DVBitManipulator::-(void)setTarget:(id)anObject
independently of my call in the method
DVBitController::-(void)awakeFromNib
And yes... anObject is my application instance of DVBitController.
So I modified the DVController::-(void)awakeFromNib method to look like
this...
-(void)awakeFromNib
{
// removed manual call to setTarget
//[bitManipulator setTarget:self];
}
And yes... the framework still called the setTarget method with a valid.
And yes... anObject was still the application instance of DVBitController.
Anyway storing the target in a local parameter and using that when
forwarding the action worked! The method
DVBitController::- (IBAction)onBitManipulator:(id)sender
executes and all is well.
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!
cheers
Simon.
_______________________________________________
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.