Life as Clay

Cocoa Programming: Helper Objects – Challenge: Make a Delegate

with 15 comments


I am enjoying Cocoa Programming for Mac OS X ; good book, well written, pretty easy to follow. Chapter 6 (3rd edition) gets a bit ahead of itself, though. At least for programming newbies it does. Why? Well, the answer to “Challenge: Make a Delegate” is much better at explaining the concept than the examples used in the chapter. The interesting point about the delegate object is that it needs contain nothing other than the delegate method. That’s a far less complicated way to explain delegates than to shoehorn them into the previous projects that included IBOutlets, IBActions, etc. Here’s the skinny:

ALL you need to do is:
1. Start a new Cocoa project
2. Create a new Obj-C class. I’ll use the name WindowTalker.
3. in WindowTalker.h, add the following (which is the sample code from the book):

#import <Cocoa/Cocoa.h>

@interface WindowTalker : NSObject {
}

- (NSSize)windowWillResize:(NSWindow *)sender
					toSize:(NSSize)frameSize;
@end

4. In WindowTalker.m, add the following (which is simpler than the example code in the book):

#import "WindowTalker.h"

@implementation WindowTalker

- (NSSize)windowWillResize:(NSWindow *)sender
					toSize: (NSSize)frameSize
{
	frameSize.width = frameSize.height * 2;
	NSLog(@"The window size is %f wide and %f tall.", frameSize.width, frameSize.height);
	return frameSize;
}

@end

5. Save the files – you’re done with the code.
6. Double-click on MainMenu.xib (or .nib)
7. Click on the header of the window to select it. Go to the inspector and set the size to 200 x 400 or something else in that ratio.
8. Drag a blue object cube to the Doc window.
9. In the inspector panel, set the identity of the blue cube to “WindowTalker” or whatever you named your class.
10. Control-click on the window (that will resize) to get the connections panel. Here you need to tell the Window that the object is its delegate. Herein lies the entire point of this exercise… That object is going to act as the servant to the window to accomplish the proportional resizing. Hence, the window needs to know which object is its servant, aka delegate. So…
11. Drag from the circle next to delegate on the connections panel for the window and release the line on top of the WindowTalker object in the Doc window.
12. Save MainMenu.xib
13. Build and Run your application. Test by dragging the window.

The dragging part in step 11 will look something like this:

Dragging from the connections panel to the blue cube - the mouse is over the blue cube, ready to release

I read through the chapter and followed the instructions and was still forming my idea of what the delegate was when I came to this exercise. My first few shots at it were too complicated – I was trying to do way too much. The concept of the delegate was much clearer to me after I figured this out, so perhaps someday this post will help somebody with the same problem.

Advertisements

Written by Clay

December 4, 2009 at 21:37

Posted in Code, Objective-C

Tagged with , ,

15 Responses

Subscribe to comments with RSS.

  1. Thanks Clay,
    I had the same issues with this exercise. Your remarks were very helpful.

    wayne mitchell

    May 4, 2010 at 02:00

    • wow cheers.. it was a bit of a strange leap for me too.. i cant tell weather some of the information he puts in is for us to use or to know about.. like the struct.. i thought i had to put that in somewhere (and hadn’t a clue where) i didn’t realise it must allready exist and he was just telling us about it!!!

      Also he said “make sure it’s twice as tall as it is wide”.. surely this is the opposite of what he then says with width:200 height:100!!!

      Cheers!

      timbo

      May 19, 2010 at 14:15

  2. This tutorial did a way better job of explaining delegation. I think you’re right in that the chapter has too much “noise” and introducing extra concepts that for someone new just get in the way.

    Very clear, well done!

    Nicholas

    June 3, 2010 at 03:32

  3. You explained the use of delegate really well and I totally get the idea and how to use it after this post. Much appreciate!

    Tommy

    January 5, 2011 at 11:18

  4. Very much appreciated. Thanks Clay..

    David DelMonte

    January 10, 2011 at 16:04

  5. well that explains everything! This helped me immensely. I didn’t have a clue until I read your post. Thanks a lot Clay.

    gnishikawa

    March 9, 2011 at 10:03

  6. Do you need to declare the method in the .h file?

    arpan

    June 18, 2011 at 16:49

    • You only need to declare that a class conforms to a protocol if you’re working with delegates – you con’t have to declare the methods. You also don’t need to declare methods that aren’t public (in other words, only ones used within the class where they are written. They will only be available to other methods that are below them in the file in that case, however.).

      Clay

      June 20, 2011 at 10:22

  7. Thx for the help ;)

    Kyro

    June 23, 2011 at 20:14

  8. Even almost 3 years later, this was really helpful. Thank you for sharing.

    Scott

    March 16, 2012 at 20:12

  9. […] 「5.3 xib ファイルのレイアウトを行う」. 参考にさせてもらったページ Cocoa Programming: Helper Objects – Challenge: Make a Delegate […]

  10. ThanQ Clay !

    Sherine

    November 7, 2012 at 19:24

  11. Thanks for the hand!

    Sly

    November 22, 2012 at 01:03

  12. Thanks for your help, its really quite annoying that the authors don’t have a repository of example answers

    Luk

    December 10, 2012 at 12:44

    • Things have gotten a little better with their forums:

      Clay

      January 4, 2013 at 14:34


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: