MouseUp and NSArrayController

How do you bind the event -mouseUp:

to the -add:

NSArrayController method ? The event -mouseUp:

lives on another object, but has the value #import

'ed and is raised on the object that controls the array being monitored.

Typically with NSButton you drag a command from a button to the NSArrayController method -add:

, but obviously this is not possible with a mouse event.

- ADDED CONTENT -
MATT: Thanks for the answer, and on first reading it made sense. As a newbie to Obj-C / Cocoa with procedural and NON-GUI language (PLM51 and C51 for embedded controllers) I am having a hard time understanding the practical IBOutlets implementation and connectivity. I have no problem with buttons etc. (IE Visible things in IB), but here's what I understand: I need to declare -IBOutlet NSArrayControler * arryCtrl;

myDocuments.h in my file. Now keep in mind that the object where I am overriding the method -mouseUp

is called Canvas and in myDocuments.h. I have an adCanvas * canvas

hence I have a canvas object created by myDocument at runtime. In IB, I drag the File Owner (myDocument right) to the ArrayController and the link is established, but not with -add:

, as this option is not available. There is no object for Canvas in nib (myDocument) But in mouseUp (canvas method) if I post message to IBOutput i.e. [arrayCtrl add:self]

arrayCtrl is unknown.

Anyway, I'm sure you guys are chuckling since the answer is probably so obvious. However, I am really trying to figure it all out and realize that the problem lies with my newness in the encoding. Thanks for pointing this newbie in the right direction.

+1


source to share


2 answers


The typical pattern here is for your controller to be a delegate to your canvas object. When you create your canvas, you will also send a message to it setDelegate:self

.

Then you can define your delegate protocol with some messages:

@protocol MyCanvasProtocol
@optional
-(void)canvas:(Canvas *)canvas mouseUp:(NSEvent *)event;
-(void)canvas:(Canvas *)canvas objectSelected:(id)theObject;
@end

      



Then, in your method of -mouseUp:

your canvas, you simply send the appropriate message to the canvas delegate.

- (void)mouseUp:(NSEvent *)event {
    // some mouse up code

    if ([delegate respondsToSelector:@selector(canvas:mouseUp:)])
        [delegate canvas:self mouseUp:event];
}

      

Structuring your code in this way allows you to easily reuse that Canvas object elsewhere and does not tie it into your controller implementation. Then your controller can create whatever objects it needs on the canvas, in any way it needs.

+2


source


In the class whose method mouseUp:

you are overriding, you need to have some way of referring to the NSArrayController. This is usually done via IBOutlets

. So, create an outlet in the classroom and connect it to an array controller in IB. This way you can just do whatever you want in the method mouseUp:

:



- (void)mouseUp:(NSEvent *)theEvent
{
    [arrayControllerOutlet add:self];
}

      

+2


source







All Articles