C Struct Question

Discussion in 'Mac Programming' started by MadDoc, Nov 16, 2007.

  1. macrumors 6502

    MadDoc

    Joined:
    Apr 25, 2005
    Location:
    UK
    #1
    I'm learning OnjC and Cocoa. My C is not my strongest point. I am trying to build a simple single window app to teach me about using delegates.

    All the app needs to do is keep the window twice as tall as it is wide when it is resized. I have one class (AppController) that has been defined and set as the delegate for the NSWindow.

    This is the windowWillResize method

    Code:
    #import "AppController.h"
    
    @implementation AppController
    
    // Delegate methods
    -(NSSize) windowWillResize: (NSWindow *) sender toSize: (NSSize) frameSize
    {
    	// Make sure that the window remains twice as tall as it is wide
    	 NSSize newSize;
    	
    	newSize.width = frameSize.Width;
    	newSize.height = frameSize.width * 2;
    	
    	return newSize;
    }
    
    @end
    This doesn't compile however as I get this error:'NSSize' has no member named 'Width'.

    I don't fully understand what I've done wrong - I thought NSSize was a C struct with 2 variables (height and width).

    What do I need to change?

    MadDoc
     
  2. macrumors 6502

    Joined:
    Jun 1, 2006
    #2
    Lower case 'W' for width.
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    It's case sensitive. "width", not "Width".
     
  4. thread starter macrumors 6502

    MadDoc

    Joined:
    Apr 25, 2005
    Location:
    UK
    #4
    Ooops.

    Thanks!

    BTW, will my approach to the problem work with this method (can't check 'til I finish work in 10 hours!).

    MadDoc,
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Just use the built-in NSWindow method setAspectRatio:
     
  6. thread starter macrumors 6502

    MadDoc

    Joined:
    Apr 25, 2005
    Location:
    UK
    #6
    Is setAspectRatio a delegate method too? If not, I am just trying to learn about delegates (it's a new concept to me). Would my method (would the corrected 'W'idth) work?

    Thanks for the tip.

    MadDoc,
     
  7. macrumors 68020

    Krevnik

    Joined:
    Sep 8, 2003
    #7
    I wouldn't use it, it doesn't behave as one expects. What the OP is trying to do is the best way to keep it smooth and not run into issues with setAspectRatio: causing odd resizing behavior.

    EDIT: One thing I /would/ change is instead of calculating the height from the width, calculate the width from the height. Introduce as little deviation with where the mouse pointer is (compared to the resize control's location) as possible.
     

Share This Page