Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

towlston

macrumors newbie
Original poster
Mar 30, 2010
4
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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).
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
 

towlston

macrumors newbie
Original poster
Mar 30, 2010
4
0
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.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
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.
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
Can you suggest a strategy for accessing an object in the program delegate from a viewcontroller?

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.
}
 

towlston

macrumors newbie
Original poster
Mar 30, 2010
4
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.