#4 Understanding delegates
In this episode, I’ll show you how to change a class behavior by implementing a delegate method.
- Download .mov file (18.7 MB, 3:46)
- Download .m4v file for iPod & Apple TV (7.4 MB, 3:46)
- This episode on Vimeo (HD quality)
- Full episode source code
Some useful links :
Update : there was an error in this episode (thanks to Giulio Cesare for pointing this out), the movies and code have been updated. If you already downloaded them, please download them again.


I’ve watched this a couple times and it inspires me keep playing. Good Job.
Why do you delegate self?
and
How does the delegate know which method to use/overwirte?
@Frank :
> Why do you delegate self?
I set the delegate to self to keep things simple.
You could create another object that would act as the delegate for the panel, but it wouldn’t be useful in this simple example.
> How does the delegate know which method to use/overwirte?
Here : http://bit.ly/gT6mU , you can see that CPWindow tests if it’s delegate object responds to windowShouldClose
Thank you, I’m not sure I get it though. ( I’ve just bought and obj-c book, maybe it will help me)
But I’ll say what confuses me, maybe it can help someone else who is foreign to obj-c programming like me.
What I understand is that delegate is like subclassing to overwrite a method, but without subclassing the class (in C, or python). You overwrite the method on the class instance. Is it correct?
But what happen if you have two different panels in your AppController that needs to delegate a difefrent windowShouldClose for each of them?
By the way, nice job with your screencast.
@Frank : you can simply test which window was passed as an argument in the windowShouldClose:(id)window method :
if (window===myWindow1)
// perform an action
else
// perform another action