嗨我在iPhone上玩弄位置,從一開始就遇到問題。我已經本地化我的問題,並確定它是CLLocationManager
這是困擾我。CLLocationManager從不調用委託方法
所以我開發了非常簡單的應用程序。我只有一個視圖控制器,裏面有一個CLLocationManager
。在視圖上加載我初始化CLLocationManager
並開始更新。我還實施了兩種方法didFailWithError
和didUpdateToLocation
。
我已經閱讀了很多問題,我學到的東西很多,所以這個價格是這樣的。在初始化期間,您必須保留CLLocationManager
。此外,它是明智卸載的view
過程中設置CLLocationManagers
delegate
到nil
(一些與傳遞給CLLocationManager
因爲框架保持它的消息做的,它從來沒有正確地釋放)
所有的一切我都無法找到什麼像樣的解釋如何使其工作。
這裏是我的代碼,所以如果有人可以弄明白,我將不勝感激。 viewControllers
頭文件
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface CoreLocationViewController : UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *locManager;
}
@property (nonatomic, retain) CLLocationManager *locManager;
@end
的viewController .m文件
#import "CoreLocationViewController.h"
@implementation CoreLocationViewController
@synthesize locManager;
- (void)viewDidLoad
{
[super viewDidLoad];
self.locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
[self.locManager startUpdatingLocation];
self.view.backgroundColor = [UIColor grayColor];
}
#pragma mark -
#pragma mark Location Delegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"in fail with error");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"in update to location");
}
#pragma mark -
#pragma mark Memory Management
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload
{
self.locManager.delegate = nil;
[super viewDidUnload];
}
- (void)dealloc
{
[locManager release];
[super dealloc];
}
@end
BTW:我使用的是iOS 4.2。我指出這一點,因爲我讀的是蘋果已經改變了位置的代表在iOS 4的
你在這裏泄漏,因爲self.locManager = [[CLLocationManager alloc] init];保留在使用alloc init獲得的引用計數之上的對象。 – logancautrell 2011-05-12 18:32:01