0

I'm stuck in this for some time now... I'm not so experienced with coding but I'm trying...

In my app I use the BOOL backFromBack like this in the AppDelegate.h:

BOOL backFromBack;

Then I set it to yes in the .m file when application enters background.

Now I want to use an

if (backFromBack)

In my viewController but how can I pass this BOOL from the delegate to the viewController?

Any help is great!

wkberg
  • 25
  • 6

1 Answers1

1

In your viewcontroller,

#import AppDelegate.h

You can then use

[(AppDelegate*)[[UIApplication sharedApplication] delegate] backFromBack]

to access your variable, assuming that the variable is accessible from the outside (i.e. synthesized)

JiaYow
  • 5,207
  • 3
  • 32
  • 36
  • Ok I got it working but my thing is, when I use `code - (void)applicationDidEnterBackground:(UIApplication *)application { backFromBack = YES; } ` and in my viewController I use the bool to set some actions with: `code if(myAppDelegate.backFromBack) { .... } ` then those actions aren't fired... How can I fix this? – wkberg Feb 21 '12 at 20:16
  • You mean, you put your app into backgroundmode, then activate it again and backFromBack is still set to NO? Maybe you're setting backFromBack to YES somewhere in one of the activation delegate functions? – JiaYow Feb 21 '12 at 20:38
  • Yes I exactly mean that. But I have checked every bit of code and the problem is not that it isn't set correctly. in the didFinishLaunchingWithOptions I set it to NO then if my app goes into background mode it is set to YES. I've checked it and it is set correctly. but in my viewController it doesn't still doesn't call the if(backFromBack) – wkberg Feb 21 '12 at 21:04
  • waait wait, is the code you're expecting to fire in the init or viewDidLoad or something like that in the viewController? If it is, those functions are not necessarily called each time you come from the background, since iOS preserves the state when you enter background. Maybe that's your problem? In short, check if the function containing your if-statement is called. – JiaYow Feb 21 '12 at 21:32
  • Ahhh yeah that's the problem. But I want to set something in the viewController when my app comes from the background. how can I access my 'btnStart' which is set in my viewController, in my AppDelegate? – wkberg Feb 22 '12 at 16:45
  • How about calling a method of the viewController in `- (void)applicationWillEnterForeground:(UIApplication *)application` or `- (void)applicationDidBecomeActive:(UIApplication *)application`? – JiaYow Feb 22 '12 at 16:57
  • Yes I tried that with `- (void)applicationWillEnterForeground:(UIApplication *)application` But It isn't called when my app comes from the background when it is in the viewController... (I'm sorry not super experienced with it) – wkberg Feb 22 '12 at 17:41
  • I might not be clear on the last comments but if I call a method from the viewController: `[viewController.btnStart setTitle:@"TEST" forState:UIControlStateNormal];` then it is never set to test... – wkberg Feb 22 '12 at 17:59
  • Made a few mistakes with placements of code... Now I just called the methods I needed from applicationDidEnterBackground. But still your answer to my first question helped me out! Thank You so much! – wkberg Feb 22 '12 at 18:56
  • Another solution for the problems I then had between my viewController and delegate use this: [http://stackoverflow.com/questions/3300694/how-to-refresh-uitableview-after-app-comes-becomes-active-again/3301277#3301277] – wkberg Feb 22 '12 at 19:59