2013-03-06 51 views
0

我想創建一個將獲取位置和地址的NSObject,我也將運行一些其他代碼。我想把它放到我的不同應用程序中。我在更新位置時遇到問題。我有協議和委託工作,但我會發布我的所有代碼,以嘗試清楚。NSObject內的CLLLocationManager

在我getLocation.h

@protocol getLocationDelegate 
- (void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude; 
@end 

@interface getLocation : NSObject <CLLocationManagerDelegate> { 
    CGFloat latitude; 
    CGFloat longitude; 
    NSString *address; 

} 

@property (nonatomic, strong) id<getLocationDelegate> delegate; 
@property (nonatomic, retain) CLLocationManager *locationManager; 

- (void)getLocationInfo; 

在我getLocation.m

- (id)init 
{ 
    if (self = [super init]) { 
     self.locationManager = [[CLLocationManager alloc] init]; 
     self.locationManager.delegate = self; 
     self.locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 


    } 
    return self; 
} 

- (void)getLocationInfo { 

    [self.locationManager startUpdatingLocation]; 
// This is being called but not starting the locationManager 
} 

後,我得到的地址和位置,我稱之爲協議這樣的,在我的getLocation.h

[self.delegate getLocation:address getLongitude:longitude getLatitude:latitude]; 

在我的.m文件中,我想獲取位置,這是代碼

getLocation *location = [[getLocation alloc] init]; 
[location setDelegate:self]; 
[location getLocationInfo]; 

然後,我有我的方法,當委託被調用

-(void)getLocation:(NSString *)address getLongitude:(CGFloat)longitude getLatitude:(CGFloat)latitude { 
    // Do code after location and address has been returned 
} 

現在我的協議和委託正在我的問題是什麼,我不能得到的LocationManager開始更新。不知道發生了什麼事。

回答

0

我想通了,ARC在CoreLocation開始之前發佈getLocation *location = [[getLocation alloc] init];。所以我只需要在我的.h中聲明它,並且它很強大。