properties

Discussion in 'iPhone/iPad Programming' started by liptonlover, Nov 1, 2008.

  1. macrumors 6502a

    Joined:
    Mar 13, 2008
    #1
    I didn't have to learn properties before, which was fine because I didn't understand them anyways. But Apple seems to be using them more and more to set/get class attributes, especially for the iPhone. I've been going through apple's doc, I understand what they are. But I don't know how to use them. I know how to declare them in a header file, but how do I then access them from a different class?
    Thanks, Nate
     
  2. macrumors regular

    Joined:
    Mar 24, 2008
    #2
    Using them is the easy part. Lets take the frame property of a UIView

    Code:
    // These are the same
    CGRect frame = [myView frame];
    CGRect frame = myView.frame;
    
    // So are these
    [myView setFrame:someFrame];
    myView.frame = someFrame;
    
    Using @synthesize generates getters and setters in the above format, and setting and getting properties actually call these methods. So the method setFrame: gets called with you the "myView.frame = someFrame" syntax.
     
  3. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #3
    Thanks, that's a great start. I almost understand all that. But just to make sure, can you please show me how to solve my specific situation? I have a custom class that I'm using as an appcontroller. It has an outlet for my UIView. The UIView class has an opacity property, and I want to change it to .5. How do I do this, exactly? I'm still not clear on that.

    Thanks, Nate
     
  4. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #4
    view.opacity =0.5;

    or

    [view setOpacity:0.5];
     
  5. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #5
    wow... that's easy. Thanks!

    I bumped into one problem though.

    this works...
    Code:
    [view setAlpha:0.0];
    but this doesn't.
    Code:
    view.alpha=0.0;
    I get the following error.
    error: request for member 'alpha' in something not a structure or union.

    So the problem is solved, but now I want to know what went wrong.
    Thanks, Nate
     
  6. macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #6
    Assuming that view is a UIView that should work.

    Are you linking against the UIKit ?

    I assume that you are as all the Classes that you create in XCode are linked against the UKit automatically.

    Top of the header file:

    #import <UIKit/UIKit.h>
     
  7. thread starter macrumors 6502a

    Joined:
    Mar 13, 2008
    #7
    It's definitely linked... besides if the first way of doing it worked, doesn't that mean it's linked anyways?
     
  8. macrumors regular

    Joined:
    Mar 24, 2008
    #8
    This usually has to do with a variable that is declared of a type that doesnt support that property. For example, settings the value of slider, but that slider is referenced by a variable declared as a UIView. It will let you call a method to set it, but not a property. Without seeing the rest of your code, that's the best help I can give you.

    But really, you dont HAVE to use the property syntax. If you feel more comfortable, you can use the getter and setter method created for you by @sythensize
     

Share This Page