Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 9, 2007, 09:56 AM   #1
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
A simple Cocoa delegation question

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.
Soulstorm is offline   0 Reply With Quote
Old Feb 9, 2007, 10:52 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   0 Reply With Quote
Old Feb 9, 2007, 05:26 PM   #3
bronxbomber92
macrumors regular
 
Join Date: Nov 2006
Can someone explain delegation? I've never understood it

(sorry for hijacking your thread )
bronxbomber92 is offline   0 Reply With Quote
Old Feb 10, 2007, 02:34 AM   #4
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
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.
Soulstorm is offline   0 Reply With Quote
Old Feb 10, 2007, 07:05 AM   #5
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by bronxbomber92 View Post
Can someone explain delegation? I've never understood it

(sorry for hijacking your thread )
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.
Eraserhead is offline   0 Reply With Quote
Old Feb 10, 2007, 07:22 AM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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.
robbieduncan is offline   0 Reply With Quote
Old Feb 10, 2007, 07:43 AM   #7
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by robbieduncan View Post
<lots of good points>
Well I'm only a beginner .
Eraserhead is offline   0 Reply With Quote
Old Feb 10, 2007, 04:34 PM   #8
bronxbomber92
macrumors regular
 
Join Date: Nov 2006
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?
bronxbomber92 is offline   0 Reply With Quote
Old Feb 10, 2007, 06:00 PM   #9
Eraserhead
macrumors G4
 
Eraserhead's Avatar
 
Join Date: Nov 2005
Location: UK
Quote:
Originally Posted by bronxbomber92 View Post
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.
Eraserhead is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
XCode - Cocoa-applescript question djjasonsa Mac Programming 3 Feb 21, 2013 03:50 PM
Need Assitance in creating a simple Cocoa OS X application lilabila Mac Programming 2 Aug 18, 2012 12:52 PM

Forum Jump

All times are GMT -5. The time now is 01:08 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC