0

I'm trying to tell a CLLocationManager object that is a property of a viewcontroller to stop updating location when the appDelegate recieves the message that the app will enter background and my problem is that I'm not sure how to refer to the viewController object inside my appDelegate class.

karthik
  • 17,453
  • 70
  • 78
  • 122
Mppl
  • 941
  • 10
  • 18

2 Answers2

2

It sounds like you're taking the wrong approach. Since the location manager is an instance variable on the view controller, it should be the view controller instructing it to stop - not the app delegate.

This is how Cocoa/UIKit/Objective-C are designed to work and anything else is an uphill fight.

Perhaps something like this in your viewController:

@implementation MyViewController

- (id)init
{
  ...

  self.locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  [self.locationManager startUpdatingLocation];

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

  ...
}

- (void)dealloc
{
  [self.locationManager stopUpdatingLocation];
  [[NSNotificationCenter defaultCenter] removeObserver:self];

  ...
}

- (void)applicationWillEnterForeground:(NSNotification *)notif
{
  [self.locationManager startUpdatingLocation];
}


- (void)applicationDidEnterBackground:(NSNotification *)notif
{
  [self.locationManager stopUpdatingLocation];
}

@end

But to answer your specific question, from inside your view controller you can use this to access the app delegate:

[UIApplication sharedApplication].delegate

That will let you to tell it about the view controller. But be careful as you may create a memory leak doing this!

You need to make sure the delegate doesn't retain your view controller, or else it will never be deallocated. And you need to make sure the delegate's reference to the view controller is set to nil when the view controller is deallocated.

In general you should avoid having the app delegate know anything at all about any specific view controllers.

Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
  • Thanks for your help but my question is the other way around. I need a way to refer to my viewController inside the appdelegate so I can send a message to my viewcontroler when the applicationdidenterbackground method is called. The code you posted I think will do just fine but it would be simpler to send a message to the viewcontroller from the appdelegate telling the viewcontroller that the appwillenterbackground message was sent to appdelegate. Thank you – Mppl Dec 31 '11 at 01:30
  • Your view controller needs to send a message to the app delegate to tell it about the controller. For example: `[UIApplication sharedApplication].delegate.myViewController = self;` But unless you do it right, this will lead to memory leaks and/or crashes, so try not to do it unless you have a good reason (perhaps you can explain why in your question). – Abhi Beckert Dec 31 '11 at 08:53
  • The reason is simple I have a CLLocationManager instance in my viewcontroller and as the view loads I want It to startupdatinglocation and I have to tell it to stop at some time...the other delegate method I have in my viewcontroller is viewdidunload but I dont want to stop updating location when the app enters background so I have to somehow refer to my CLLocationmanager instance inside the appwillterminate method (wich is implemented in my appdelegate) to tell it to stopUpdatingLocation. – Mppl Jan 01 '12 at 23:11
  • I think your question is valid, I got into the similar situation too. I have a Facebook object in my appDelegate, and my ViewController implements its FBSessionDelegate. When I allocate FB object i need a reference to the ViewController to set it as FB object's delegate. You may suggest that i can put FB object into ViewController, but i also need to refer it in handleOpenURL function. – jAckOdE Mar 12 '12 at 01:52
0

Isn't the viewWillDisappear delegate method not called in your viewcontroller when app is about to enter background ?

Lefteris
  • 14,550
  • 2
  • 56
  • 95