pretty noob question..

Discussion in 'iOS Programming' started by aly.yousuf7, Mar 2, 2011.

  1. aly.yousuf7, Mar 2, 2011
    Last edited by a moderator: Mar 2, 2011

    aly.yousuf7 macrumors newbie

    Joined:
    Mar 2, 2011
    #1
    i'm new to obj-c and new to iphone dev..i'm making an iphone application in which i want to store a string in a variable and use it in different files..(different ViewControllers)
    here is what i have done.. and what i have to do..:

    i have this in "Vars.h" file:
    Code:
    extern NSString* AccessToken;
    and then i have this in "Vars.m" file:
    Code:
    NSString * AccessToken = @"false";
    now in other file "LoginViewController.m", i have this:
    Code:
    #import "Vars.h";
    //......
    -(IBAction) task {
      // i have another NSString variable here named "var2"... i want the content of that var2 variable to be copied to AccessToken...how to do that..?
    }
    sorry for bad communicating skills.. =/
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    There are right ways to do this and wrong ways. First one of the wrong ways

    Code:
    AccessToken = var2;
    Problem with that is there is no retain when you set AccessToken and no release of the old value of AccessToken when it gets its new value.

    A right way to do this is with an @property. You can add a property to one of your classes, lets say the app delegate. Then it becomes simple.

    Code:
    // App Delegate.h
    @property (nonatomic, copy) NSString* accessToken;
    
    // Login View Controller.m
    appDelegate.accessToken = var2;
    All the retain/release stuff is taken care of for you automatically this way. The owner of the accessToken doesn't have to be the app delegate. It could be another object of another class.
     
  3. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #3
    That code has a smell...

    ... of global variables.

    Stop it.
    Right now.
    Break the habit before it gets stuck.

    Here's a better way: if you need to pass an object (your NSString) among a set of other objects (your view controllers), then create that object in your application delegate and PASS IT to those view controllers. You don't even have to have an ivar for it - create it on the stack in application:didFinishLaunchingWithOptions: (with alloc/init) and don't release it. Now it's around for good.

    Pass it to your view controller using a property of the view controller. Don't use copy, use retain (make sure you release it in that view controller's dealloc method). Now it's kind of like a delegate object to all those view controllers - all of them have the same reference (you have to be careful about synchronization and the like but for now just ignore that). One view controller will change the value, and since they all have a reference to the same thing, the others will see the updated value.

    Extending a little on Phoney's approach, the simplest way is to change the view controller, much like this:
    Code:
    @interface AlysViewController : blahblahblahViewController
    
    ...
    
    {
    
    ...
    
        NSString *var2;
    
    ...
    
    };
    
    ...
    
    @property (retain) NSString *var2;
    
    ...
    
    @end
    
    @implementation AlysViewController
    
    ...
    
    @synthesize var2;
    
    ...
    
    -(void)dealloc
    {
    
    ...
        [var2 release];
        [super dealloc];
    }
    @end
    and then something like this in the application delegate's application:didFinishLaunchingWithOptions: method
    Code:
    @implementation AlysApplicationDelegate
    
    ...
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    ...
        self.viewController.var2 = accessToken;
    ...
    }
    
    ...
    
    @end
    This way, the view controllers's var2 is a reference to the same object as the application delegate's accessToken, and 2henever a value is written to var2, it's really writing to accessToken.

    If you want changes to occur to var2 WITHOUT saving to accessToken until the user taps Save or Done and not do it if they tap Cancel, then var2 and accessToken are just separate properties of the viewController, and when the user taps Save do that the assignment.
     

Share This Page