开发者

variable accessing

I have a variable x in one class.And I want to access the 开发者_如何学运维updated x value in some other class. There is so much of confusion.Can I use property?.Please help me.

Thanks in advance


Do you mean that you want to be told when the value changes? Have a look at Key Value Observing


To simply access an iVar in one class from another, a property is exactly what you want.

The syntax is : in your .h

@interface myclass : NSObject {

    UIWindow *window;
}

@property (nonatomic, retain) UIWindow *window;

@end

in your .m

@implementation myclass
@synthesize window;

...

@end

The @synthesize directive instructs the compiler to produce a lot of boilerplate code (as directed by the (nonatomic, retain) specifiers. In this case to handle thread safety and memory management.

Also note that in Objective-C 2.0 the iVar declaration UIWindow *window; is not required.

If you want to be notified in your second class when an iVar is updated, you need to look at key value observing. Unless you are writing a framework or some very dynamic code, that is probably overkill.


Maybe this tutorial will help you out..

If this is not what you mean, please rephrase the question, because i don't understand it..

Edit: Or a shared Instance can be used


you could access it by #import Classname, and then just use the getter that is created with the property. but first initialize the class you have imported..

#import "ClassY.h"
@implementation ClassX

ClassY * classY;
NSString * name;

...

name = [classY name];

...

@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜