PDA

View Full Version : A simple Cocoa delegation question




Soulstorm
Feb 9, 2007, 09:56 AM
I have an NSMatrix of switch buttons. I just want to have my application do something when one of these switch buttons are pressed with the mouse. Can anybody help me on this?

Here's what I have tried: Subclassing NSObject in IB and making the newly created object a delegate of the NSMatrix. But what delegation method should I use? I have tried a few, but none of them work.

Any assistance would be appreciated.



robbieduncan
Feb 9, 2007, 10:52 AM
Delegation is not used for actions. You need to set a method in your object as the action target and within that check which cell in the Matrix was clicked.

bronxbomber92
Feb 9, 2007, 05:26 PM
Can someone explain delegation? I've never understood it :confused:

(sorry for hijacking your thread :o )

Soulstorm
Feb 10, 2007, 02:34 AM
It's ok, I have already gotten my answer.

Delegation is a term to explain events that happen after another event has happened. Simple IBActions do not work that way.

For example, when you close the window, a -windowWillClose delegate function is called. NSView has delegate functions that are invoked when the mouse is over the view, when the mouse is clicked, etc.

In order to use delegate functions, you must write the delegate function you want by writing the function declaration (you can find the declarations and supported delegate functions of each class into the ADC Library) into the implementation file of an object that exists in Interface Builder, and then give any commands you want. In Interface Builder, you must have set the object as delegate of the object you want.

Hope that helps.

Eraserhead
Feb 10, 2007, 07:05 AM
Can someone explain delegation? I've never understood it :confused:

(sorry for hijacking your thread :o )

Delegation records when stuff changes without an action, this is useful when the values are bound with Cocoa Bindings to update other values, or when switching values in a table/tab view, so you can update values based on which entry in a table is selected, allowing you to have different properties for each. for example Safari and Firefox use it to change the size of the preferences window.

robbieduncan
Feb 10, 2007, 07:22 AM
Delegation can be and is used for so much more than is described above. Delegation is a general purpose design pattern (http://en.wikipedia.org/wiki/Delegation_pattern) allowing an object to influence, alter or respond to the behaviour of another class without subclassing.

In the standard Cocoa classes delegation can be used to receive messages when actions are performed (although you could simply register for the notifications instead). It can also be used to customise the behaviour and appearance of some of the core UI classes. The normal example of this is to customise the attributes of the cell in a table view. This used to be the way to implement striping of rows before Apple made that built in.

Eraserhead
Feb 10, 2007, 07:43 AM
<lots of good points>

Well I'm only a beginner ;).

bronxbomber92
Feb 10, 2007, 04:34 PM
Thanks, that helped a lot! So, put simply, delegation is a method that happens after a some other certain action happens (such as a mouse click or someone attempting to close a window).

How do you make it respond to delegate or write a delegate method?

Eraserhead
Feb 10, 2007, 06:00 PM
How do you make it respond to delegate or write a delegate method?

The documentation for the specific object you are using normally provides details. (I know it's a RTFM answer, but it really is the most useful)
But so I'm not being at total RTFM person for example

- (void)tabView:(NSTabView *)tabView
didSelectTabViewItem:(NSTabViewItem *)tabViewItem
Runs when a tab View item in the tab view with the delegate is selected. And


-(void)controlTextDidEndEditing:(NSNotification *)aNotification


Runs when a text box or other control has finished being edited.

These go in the delegate class file.