Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
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

macrumors regular
Mar 24, 2008
108
1
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.
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
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

macrumors 65816
Sep 8, 2005
1,005
0
London , UK
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

macrumors 6502a
Original poster
Mar 13, 2008
989
0
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
 

MacDonaldsd

macrumors 65816
Sep 8, 2005
1,005
0
London , UK
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

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

macrumors 6502a
Original poster
Mar 13, 2008
989
0
It's definitely linked... besides if the first way of doing it worked, doesn't that mean it's linked anyways?
 

SqueegyX

macrumors regular
Mar 24, 2008
108
1
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.