noob question: sharing variables between ViewControllers

Discussion in 'iOS Development' started by cybohemia, Jan 27, 2009.

  1. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    I have a field, num, in a MainViewController object. I set it to 100 in an action that responds to a button. The action also switches to a SecondViewController object, which has a pointer to the MainViewController object. But now, when I look at num through the pointer, it is (null) instead of 100.

    This would be straight-forward in C++ or Java but I'm wondering if I'm missing something basic as I pick up Objective C while working through tutorials for the iPhone.

    Any help would be greatly appreciated!

    (I can post some sample code if that would be helpful - I don't know if there are just some general pitfalls I should know about for doing what I'm trying to do.)
  2. spykr

    spykr Banned

    Joined:
    Jan 4, 2009
    Messages:
    109
    Likes Received:
    0
    Make an NSObject file (call it Global), so you have Global.h and Global.m.
    In Global.h, above where it has
    NSObject blah {
    }
    add your variables.
    Example:
    NSString *string;
    int blah;
    UIView *view;
    Then, in the viewcontrollers you want to use these, add #import "Global.h" to the top.
    Try it out and tell me if you're having any problems :].
  3. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    I had tried to make a class and I used [Shared alloc] in each ViewController's loadView method. Maybe that's not the way to go...

    In your example, how would I instantiate a global object in each of the classes that needs to access a field in it?

    Thanks!
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Out of all the different ways to do this, the easiest would be to simply pass the variable to your second view controller when that button is pressed. Barring that, create a property in your second view controller, and set it when that button is pressed. Or, have the app controller, or whatever owns your view controllers, control the variable and either pas it to the second view controller when that button is pressed, or have the second view controller read that variable on demand. If all else fails, save that variable ad a setting using NSUserDefaults.

    There are several other methods, but try these first--in the order listed.
  5. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    My problem is in the mechanics of how to access the shared data. I've tried creating a third class shared by the first two classes. Since the pointers from the first two classes to the third class always begin as (null), I initialize an instantiation and I think that's where I'm doing something wrong.

    When I do a [[Shared alloc] init], in object A, the shared object is created there where I can set the data. But when I do it again in object B, the shared object is re-initialized so I lose the data.

    Is there a right way of doing this?

    Thanks!
  6. spykr

    spykr Banned

    Joined:
    Jan 4, 2009
    Messages:
    109
    Likes Received:
    0
    Or you could set it all up in a global file >.>
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Generally, you want to avoid globals. There is nothing wrong with what I said. Do any one of the methods I told yo, and you will be fine.

Share This Page