开发者

Accessing variables of another class in xcode

I have a balloonGameViewController.h and another class I made called balloon.h

I want t开发者_开发技巧o access some variables I set in balloon.h from the viewController

Is there any way I can achieve this?


How are your variables set in ballon.h? You should use @property to declare variables that you want other classes to be able to access. Then, you can access them either by treating them as a method, or dot notation:

myObject.variable;

myObject should be an instance of type balloon, which can be created by importing the balloon.h and initializing a new instance, if you do not already have one.


Just import the balloon.h file into your balloonGameViewController

#import balloon.h

and then access the variables as usual, assuming they are public. Otherwise you have to make them public or create getters and setters.


As others said, you'll have to #import baloon.h. But you did not say if these variables are global variables or ivars of a class. If they are ivars, you'll first have to find the instance of the class (the object) of which they are ivars. If you have that, and they are public or properties, you can access them as members of that object.

IOW, it is hard to tell if you don't tell us what kind of variables in balloon.h you want to access. But, see above.


i don't know if i got your question well, but i faced that once upon a time , i couldn't access the variables via (.) operator but via (->)

in my case there were 2 classes : MenuCalss , and ToolsClass ;

in ToolsClass.h :
@public
    bool ToolBarVisible;

//in MenuCalss there was a ToolsClassObject. ToolsClassObject is an instance of type ToolsClass, which can be created by importing the ToolsClass.h and initializing a new instance.

, and the access way in MenuClass.m is :

ToolsClassObject->ToolBarVisible = false;


Using your XCode you need to make import, declare the property, and then use "object.variable" syntax. The file "balloonGameViewController.m" would look in the following way:

#import balloonGameViewController.h
#import balloon.h;

@interface balloonGameViewController ()
...
@property (nonatomic, strong) balloon *objectBalloon;
...
@end

@implementation balloonGameViewController

//accessing the variable from balloon.h
...objectBalloon.variableFromBalloon...;

...
@end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜