我是iOS開發新手,我正在編寫3視圖位置應用程序。 第一個視圖是應用程序的主視圖,第二個視圖是包含多個位置的表視圖,第三個視圖是詳細視圖,用戶可以在其中編輯或向表視圖添加新位置。XCode ARC同一視圖的多個實例
我在第一個和第三個視圖中使用CLLocationManager,但兩個視圖都有自己的CLLocationManager實例,因爲對於詳細視圖,我需要最佳精度,而在MainView中,我不需要最佳精度。
所以這裏的問題是: 在我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方法攔住他)
- (void) enteredForeground: (NSNotification*) notification {
[self.locationManager startUpdatingLocation];
}
我正在使用XCode 4.2與ARC。 問題是,如果我訪問了大約10次DetailView,轉到背景(從MainView f.e.),然後我再次進入前景,然後10 LocationManagers將立即啓動(這是我的NSLog說)。 對於DetailView(和其他視圖),似乎存在相同數量的實例,例如這些視圖的訪問次數。
也許這些視圖如果消失,可能是因爲NSNotification?
如果有人能夠幫助我解決這個問題,我將不勝感激,因爲如此多的LocationManagers會給電池帶來很大壓力。
在此先感謝!
是的,我已經這樣做。方法didEnterBackground調用stopUpdatingLocation .... – android