hillegass Chapter 6 make a delegate challenge. help please

Discussion in 'Mac Programming' started by simon the diver, Jan 26, 2010.

  1. simon the diver macrumors newbie

    Joined:
    Jan 26, 2010
    #1
    hi folks, im a 42 year old scuba diver bum with a 21 year old computer degree - they taught me cobol !

    anyway as part of my transition out of the dive industry im tying to teach myself to program again - and its much harder than when i self taught myself assembly langauge on the vic 20 when i was a teenager

    ANyway - ive ploughted thru the apress c and objective c boossand am hitting walls in Hillegass

    chapter 6 has 2 exercises - i'm having trouble with the first one - this delegate thing is doing my head in. i get the concept of Delegate methods executing when something has happened.

    heres the text from the book - page 109


    Challenge: Make a Delegate
    Create a new application with one window. Make an object that is a Delegate of the window. As the user resizes the window, make sure that it always remains twice as tall as it is wide.



    my problem is not knowing where to start . ive create a new ap. fine. simple - its the "make an object that is a delegate of the window" . can someone talk me through that linking stage or send me there solution to look at - the coding bit that actually resizes the window i'll be ok with - its that first step





    the rest of the challenge


    Here is the signature of the Delegate method you will implement:

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

    The first argument is the window being resized.
    The second argument is a C struct that contains the size that the user has asked for:

    typedef struct _NSSize {
    float width;
    float height; } NSSize;


    Here is how you create an NSSize that is 200 points wide and 100 points tall:
    NSSize mySize; mySize.width = 200.0; mySize.height = 100.0; NSLog(@"mySize is %f wide and %f tall", mySize.width, mySize.height);
    You can set the intial size of the window in the Size Inspector in Interface Builder.
     
  2. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #2
    Typically becoming a delegate is twofold.

    For example:
    Code:
    @interface SomeObject : NSObject <UIApplicationDelegate> {
    ...
    }
    @end // sorry its iPhone code.
    
    That basically says "hey SomeObject will have some/all methods defined in the UIApplicationDelegate"
    NSWindowDelegate Protocol is here
    http://developer.apple.com/mac/libr...dowDelegate_Protocol/Reference/Reference.html

    Then you need to set an instance of SomeObject to be the delegate.
    NSWindow has a methond that looks like...
    Code:
    - (void)setDelegate:(id < NSWindowDelegate >)delegate
    So either use that methond with the instance of your delegate object, or if you're using a NIB file, link the delegate outlet to your object.



    What it all means is that when one of the things in <NSWindowDelegate> happens, it will look to its delegate to see if its possible, or if you want to change the behavior.
    The method you are overriding looks like.
    Code:
    - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
    The return type (NSSize) is the size it WILL ACTUALLY resize to. Meaning NSWindow says "hey look at this size (NSSize)frame and tell me what you think" Then you look at the values and say "I'd rather you become (NSSize)thisSize instead"
     
  3. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #3
    You can streamline the process in Interface Builder. If your window lives in a nib file, you can instantiate the delegate object in the nib and connect the window directly to it with no extra coding.
     
  4. ChOas macrumors regular

    Joined:
    Nov 24, 2006
    Location:
    The Netherlands
    #4
    In IB, set your ApplicationController as the delegate for your window, then implement the function in there

    I did it as such:

    Code:
    - (NSSize) windowWillResize:(NSWindow *) sender toSize: (NSSize) frameSize {
    	
            NSLog(@"Resize requested x:%f, y:%f",frameSize.width, frameSize.height);	
    	NSSize newSize;
    	
    // [..]
    // for you to implement ;)
    // [..]
    
    	return newSize;
    };
    
    
     
  5. simon the diver thread starter macrumors newbie

    Joined:
    Jan 26, 2010
    #5
    Thanks folks - sorted - advice for other stumbling newbies

    advice deeply appreciated folks. thanks you for taking the time and making the effort.

    for the sake of other newbies who may have been stumbling at the same point....

    it was as simple as control clicking on my appController object and dragging from the circle at the end of the "new referenced object" line in the pop up to the window. when you let go of the mouse a little pop up appears with the word delegate on it . click that and its done

    i had been looking for an option in the first pop up that said delegate there isnt - you are looking for new referenced object.

    thats all there is to it . then you just write the code for the delegate method windowWillResize, returning the new NSSize, in appController.m. and when you run it the window resizes in the 2*1 ratio as you drag.

    your appController.h file doesnt have any code except for the stuff automatically generated - no iboutlets - nothing.

    no i'll just go and gather up the hair i pulled out and glue it back in

    the work of seconds and i wasted a day . lol
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Don't beat yourself up over it. It still happens even after decades of experience.

    The solutions to many problems are simple. The hard part is figuring out the correct solution. Applying solutions that were incorrectly or hastily figured out is frequently an avenue to long-term problems that are even more difficult to solve.

    One's skill at recognizing problems of a similar class is what experience is all about. A fast coder with no ability to recognize problems and apply known solutions is often worse than a mediocre coder with good recognition skills. The former writes more code more times over, while the latter thinks more and writes the minimum once.
     
  7. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #7
    Hardly wasted. You are learning, this is something you will not forget.
     
  8. monsieurpaul macrumors regular

    Joined:
    Oct 8, 2009
    #8
    Hey,

    I am reactivating this thread because I am doing this challenge.So far it works: I have added an IBOutlet to NSWindow, done the linking in IB and used this function for the resizing:

    Code:
    - (NSSize)windowWillResize:(NSWindow *)sender 
                        toSize:(NSSize)proposedFrameSize 
    { 
    	proposedFrameSize.height = (proposedFrameSize.width/2); 
    	NSLog(@"myWindow will resize to %f wide and %f tall", 
    		  proposedFrameSize.width, proposedFrameSize.height); 
    	return proposedFrameSize; 
    }
    However, I don't understand how windowWillResize works: how the function get the actual size of the window ?

    The function return a size where width = 2 * height but I don't set a real value for the current height or width of the window.
     
  9. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #9
    The delegate method windowWillResize:toSize: gives you a chance to change the values before they're used.

    • The user resizes the window.

    • windowWillResize is called with the proposed new size of the window, giving you the chance to alter the values, which you have done with the width. You returned the new NSSize for the window.

    • The window is resized to the size you returned.

    You did set a value for the width, calculated from the height. Both width and height, the 'real values' were passed to your method. You modified the size, then returned it.
     

Share This Page