0

I'm trying to turn off location services in applicationDidEnterBackground, messaging my Stop... method in another vc of my storyboard. I've tried the technique in: Storyboard - refer to ViewController in AppDelegate but my understanding is that this creates a new instance of the respective VC rather than a reference to the current instance, and I confirmed this by nslogging the addresses of the original controller and the reference in AppDelegate. They are different so the method does not stop my location services. Is my understanding of vc instances correct?

I see the technique used in the Regions sample code (https://developer.apple.com/library/ios/samplecode/Regions/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010726), in brief:

@class RegionsViewController;
@property (nonatomic, retain) IBOutlet RegionsViewController *viewController;
...
@synthesize viewController;
self.window.rootViewController = self.viewController;
[viewController.locationManager stopUpdatingLocation];

But this uses nibs, so I can't figure out how to convert that code. It seems to rely on originally instantiating the location VC from appdelegate, so the one and only instance occurs from appdelegate, whereas my storyboard app, if I understand it, instantiates my location VC implicitly on its own. Playing with the Regions code, logging the respective addresses from AppDelegate and RegionsViewController shows the matching values, as expected, so the location services instance is stopped. So can someone tell me the storyboard equivalent of the Regions technique?

Thanks

Community
  • 1
  • 1
Jack Bellis
  • 1,177
  • 8
  • 10
  • self.window.rootViewController should give you a reference to whatever is the current root view controller, it doesn't matter whether that controller came from a xib or storyboard. So does the code you posted not work? – rdelmar Feb 17 '14 at 05:32
  • The code I posted was right out of the Regions sample code and no, I could not get it to work in my app. I did not include the line that assigns rootviewcontroller because I wasn't trying to change my rvc, which is a tabbarcontroller. Maybe you're saying I get the root then get its components. I'll work on that. Thanks – Jack Bellis Feb 17 '14 at 14:51

1 Answers1

1

The following worked (in appdelegate). At least I see the same address nslogged for the final controller reference in AppDelegate and when directly retrieved in the referenced vc... and the location services arrow disappears when going to background (after a random lag, as appears to be the expected behavior).

My vc structure was (root)Tabbar>Navcontroller>Nearby (my location controller).

UITabBarController *tabBarController =(UITabBarController *)self.window.rootViewController;

UINavigationController *navigationController = [tabBarController viewControllers][0];

Nearby *nearbyInstance = (Nearby *)[navigationController topViewController];
[nearbyInstance stopAllLocUpdateModes];

Thanks rdelmar, I guess you gave me enough to keep on that track.

Jack Bellis
  • 1,177
  • 8
  • 10