PDA

View Full Version : properties




liptonlover
Nov 1, 2008, 06:46 PM
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



SqueegyX
Nov 1, 2008, 10:20 PM
Using them is the easy part. Lets take the frame property of a UIView

// 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.

liptonlover
Nov 2, 2008, 07:38 AM
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

MacDonaldsd
Nov 2, 2008, 08:18 AM
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

view.opacity =0.5;

or

[view setOpacity:0.5];

liptonlover
Nov 2, 2008, 10:51 AM
wow... that's easy. Thanks!

I bumped into one problem though.

this works...
[view setAlpha:0.0];

but this doesn't.
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

MacDonaldsd
Nov 2, 2008, 01:16 PM
wow... that's easy. Thanks!

I bumped into one problem though.

this works...
[view setAlpha:0.0];

but this doesn't.
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

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>

liptonlover
Nov 2, 2008, 05:17 PM
It's definitely linked... besides if the first way of doing it worked, doesn't that mean it's linked anyways?

SqueegyX
Nov 3, 2008, 12:17 PM
It's definitely linked... besides if the first way of doing it worked, doesn't that mean it's linked anyways?

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