PDA

View Full Version : Access instance variable from another class.




HyperSnake
Mar 31, 2009, 12:11 PM
I'm doing a flip-side application (not using the template) where I have 3 classes and the app delegate. They are:

RootViewController(.h and .m)
FrontSideViewController(.h and .m)
BackSideViewController(.h and .m)

Each have their own view. In FrontSideViewController I have declared a dictionary loading contents from a .plist file. My problem is: I don't know how I can change the contents of this dictionary while inside BackSideViewController. I'm showing a TextView and a Label in the FrontSideView(loaded from the class FrontSideViewController) and I want to be able to change the dictionary from which the TextView gets it's data from in the BackSideView(loaded from the class BackSideViewController).

In short: How do I change the value of dictionary in class X while inside class Y?

I would appreciate any help or possible alternatives. If it is not possible, how could I then do this?

- Ben



jnic
Apr 1, 2009, 07:17 AM
http://forums.macrumors.com/showthread.php?t=611602

bboyjayz
Apr 1, 2009, 07:20 AM
maybe you can code ascessor (ascesseur in french), in the class containing the dictionnary to modify, create 2 methods, first one to have access to the variable , for exemple:
-(NSArray*)getDictionnary{
return myDictionnay;
}

and the second one to modify your dictionnary, like this:
-(void)setDictionnaryTo:(NSArray*)newDico{
myDictionnary = newDico;
}

then, in the class where you want to modify the variable,
just create an object of your first class, and call setDictionnary to modify your variable "myDictionnary"

Maybe property does this work, but i'm not used to use it, i prefere ascessor, more used in other language like C++, Java etc...

I hop my answer will help you ...

iphonedevelop18
Apr 1, 2009, 09:12 AM
worst case scenario you could put both views under the same view controller.

dejo
Apr 1, 2009, 10:06 AM
There are number of ways to approach this issue. First, you could create accessors for the dictionary in your first view controller and that would allow you to use them in the second view controller. Other solutions include moving the dictionary to the root view controller or the app delegate or as a global variable. But if you want to follow the MVC paradigm more closely, you probably want to define a new model class that deals with the dictionary.

Maybe property does this work, but i'm not used to use it, i prefere ascessor, more used in other language like C++, Java etc...
Using a @property declaration and a @synthesize are not really an alternative to an accessor, they are accessors. They are just a shorthand that let's the compiler do all the heavy-lifting so you don't have to. Minimizes errors that way, too.

bboyjayz
Apr 1, 2009, 10:10 AM
Thank you to the information about @property, but it is one thing i don't understand, it is parameter like "retain" or "nonatomic", wich obliged user to have a property for Object, but if i want a property for a non-Object variable like an int, how do i do ?

dejo
Apr 1, 2009, 10:14 AM
...but if i want a property for a non-Object variable like an int, how do i do ?
@property int variableName;

bboyjayz
Apr 1, 2009, 10:19 AM
so simple ! thank you a lot

dejo
Apr 1, 2009, 10:31 AM
so simple ! thank you a lot
Hope this doesn't sound too harsh. This is pretty basic Objective-C concepts. Perhaps it's time to take a step back and regain an understanding of the language itself before doing any more real coding.