Ios XCode弧同一视图的多个实例

Ios XCode弧同一视图的多个实例,ios,cllocationmanager,xcode4.2,automatic-ref-counting,Ios,Cllocationmanager,Xcode4.2,Automatic Ref Counting,我是iOS开发新手,正在编写一个3视图位置应用程序。 第一个视图是应用程序的主视图,第二个视图是具有多个位置的表视图,第三个视图是详细视图,用户可以在其中编辑或向表视图添加新位置 我在第一个视图和第三个视图中使用了CLLocationManager,但这两个视图的每个视图都有自己的CLLocationManager实例,因为对于细节视图,我需要最佳精度,而在主视图中,我不需要最佳精度 问题是: 在我的AppDelegate.m中,我收到了一个通知,当应用程序进入前台时会触发该通知: - (voi

我是iOS开发新手,正在编写一个3视图位置应用程序。 第一个视图是应用程序的主视图,第二个视图是具有多个位置的表视图,第三个视图是详细视图,用户可以在其中编辑或向表视图添加新位置

我在第一个视图和第三个视图中使用了CLLocationManager,但这两个视图的每个视图都有自己的CLLocationManager实例,因为对于细节视图,我需要最佳精度,而在主视图中,我不需要最佳精度

问题是: 在我的AppDelegate.m中,我收到了一个通知,当应用程序进入前台时会触发该通知:

- (void)applicationWillEnterForeground:(UIApplication *)application
{

    [[NSNotificationCenter defaultCenter] postNotificationName: @"didEnterForeground" object: nil  userInfo: nil];
}
在我的第三个视图DetailViewController.m中,我在viewDidLoad中注册此通知:

- (void) viewDidLoad {

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(enteredBackground:) name: @"didEnterBackground"  object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(enteredForeground:) name: @"didEnterForeground"  object: nil];
}
DetailViewController.m中的enteredForeground方法只需要再次启动位置管理器(didEnterBackground方法阻止了他)

我使用的是带有ARC的XCode 4.2。 问题是,如果我访问DetailView大约10次,转到后台(即从主视图),然后再次进入前台,然后立即启动10个LocationManager(这是我的NSLog所说的)。 对于DetailView(以及其他视图),似乎存在与这些视图的访问次数相同的实例数

如果视图消失了,可能视图不会正常发布,可能是因为NSN通知

如果有人能帮我解决这件事,我将不胜感激,因为很多地方经理会非常强调电池


提前谢谢

我相信当它进入后台时,您需要停止更新位置。否则,它将生成多个实例。

是的,我已经这样做了。方法didEnterBackground调用StopUpdateLocation。。。。
- (void) enteredForeground: (NSNotification*) notification {
    [self.locationManager startUpdatingLocation];
}