Access instance variable from another class.

Discussion in 'iPhone/iPad Programming' started by HyperSnake, Mar 31, 2009.

  1. macrumors member

    Joined:
    Jan 5, 2009
    Location:
    Switzerland
    #1
    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
     
  2. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
  3. macrumors member

    Joined:
    Mar 20, 2009
    #3
    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 ...
     
  4. macrumors member

    Joined:
    Feb 26, 2009
    #4
    worst case scenario you could put both views under the same view controller.
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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.

    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.
     
  6. macrumors member

    Joined:
    Mar 20, 2009
    #6
    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 ?
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Code:
    @property int variableName;
     
  8. macrumors member

    Joined:
    Mar 20, 2009
    #8
    so simple ! thank you a lot
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    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.
     

Share This Page