Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Can someone explain delegation? I've never understood it :confused:

(sorry for hijacking your thread :eek: )

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

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Delegation can be and is used for so much more than is described above. Delegation is a general purpose design 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.
 

bronxbomber92

macrumors regular
Nov 23, 2006
109
0
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

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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
Code:
- (void)tabView:(NSTabView *)tabView
didSelectTabViewItem:(NSTabViewItem *)tabViewItem
Runs when a tab View item in the tab view with the delegate is selected. And

Code:
-(void)controlTextDidEndEditing:(NSNotification *)aNotification

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

These go in the delegate class file.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.