Stuck on string variables

Discussion in 'iOS Programming' started by towlston, Mar 30, 2010.

  1. towlston macrumors newbie

    Joined:
    Mar 30, 2010
    #1
    I'm relatively new to iphone programming and am stuck on a very basic issue with variables. I want to access a string variable (delegateString for this example) declared and set in my apps delegate (AppDelegate) from a view controller. So I've tried both lines of code below in my view controller to access delegateString.
    Code:
    AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSString *localString = mainDelegate.delegateString;
    
    NSString *localString = [AppDelegate *)[UIApplication sharedApplication].delegate delegateString];
    
    Both techniques work, and I can get the delegateString variable just fine in the view controller, but it's always empty. I'm sure I just don't understand how to do this. Any help is greatly appreciated, I've been stuck for two days.

    Thanks in advance.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What leads you to believe that it won't be empty? Have you set it to a non-empty string in the delegate. Post all of the relevant code (in this case the delegate class).
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Also, it could be a matter of timing. Make sure your string is being set in your app delegate before you are retrieving its value in your view controller.
     
  4. towlston thread starter macrumors newbie

    Joined:
    Mar 30, 2010
    #4
    Ah, dejo, thanks for your 'timely' post (pardon the pun). It is a timing issue. I'm a bit fuzzy on the code flow and knowing what order the implementation files are executed in xcode. Can you suggest a strategy for accessing an object in the program delegate from a viewcontroller? I've been setting the string in the applicationDidFinishLaunching method of the delegate and then accessing it from the viewdidload method of the view controller. Thanks to you, now I see why that won't work - the viewcontroller implements before the delegate. Any suggestions?
    Thanks in advance.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    First, it would help if you, as robbieduncan has already suggested, posted the relevant code, in this case, at least your app delegate's applicationDidFinishLaunching and your view controller's viewDidLoad. Also, make sure to include the mechanism for how your view controller is being loaded.

    Second, as for a strategy on where to access the object, make sure you understand the program flow (stepping through / into with the debugger can be a big help here) and put the access at a point where it is needed. For example, viewWillAppear or viewDidAppear may be better places.
     
  6. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #6
    Yes - create a property on your view controller and pass it in that way. You should rarely need to use your Application delegate as some kind of super-global; passing dependencies around makes your application structure less rigid and if you're unit testing, easier to test.

    Apple advocate a similar approach (for instance, when passing around an NSManagedObjectContext in a Core Data application). So, in your view controller:

    Code:
    @interface MyViewController : UIViewController
    {
      NSString *someString;
    }
    @property (nonatomic, copy) NSString *someString;
    @end
    
    // with an appropriate @synthesize in the implementation
    
    Then, in applicationDidFinishLaunching:

    Code:
    - (void) applicationDidFinishLaunching:(UIApplication *)application
    {
      // assuming an instance of myViewController was loaded from your
      // main NIB file
    
      // I assume you already have something like this
      self.delegateString = @"my string"
    
      // now do this:
      myViewController.someString = self.delegateString
    
      // add myViewController.view to the window etc.
    }
    
     
  7. towlston thread starter macrumors newbie

    Joined:
    Mar 30, 2010
    #7
    Thanks for the post, Luke, it makes good sense. I'm more comfortable doing it your way and will give it a try. I do find Obj C syntax a challenge but am slogging through. Thank you, all, for generously giving your time and advice.
    All the best.
    Towlston
     

Share This Page