在自己的类中的Objective-C源码locationManager

我想从自己的类中获取locationManager,我可以从其他类调用它。

我做了什么。 创build一个iPhone应用程序,并在ViewController中添加locationManger代码。 工作。

现在我创build了一个类Lokation ,将所有的locationManager代码移动到这个新类中,并从ViewController中调用该代码。 结果:函数getLocation被调用,但是locationmanager:manager didUpdateToLocation:newLocation fromLocation:oldLocation不被调用。

产量

2013-07-03 15:44:14.124 Sandbox2 [41374:c07] Inside Lokation :: getLocation

缺less什么?如何整合? 有些东西告诉我“代表”,但我是这个主题的新手。

ViewController.h

 #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController <CLLocationManagerDelegate> @end 

ViewController.m(20130704更新为工作代码)

 #import "ViewController.h" #import "Lokation.h" @interface ViewController () // 20130704 added property @property (strong, nonatomic) Lokation *lokation; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 20130704 corrected call to getLocation self.lokation = [[Lokation alloc] init]; self.lokation.delegate = self; [self.lokation getLocation]; } @end 

Lokation.h

 #import <Foundation/Foundation.h> #import <CoreLocation/CoreLocation.h> @interface Lokation : NSObject <CLLocationManagerDelegate> { CLLocationDegrees currentLat; CLLocationDegrees currentLng; } @property (strong, nonatomic) CLLocationManager *locationManager; @property (nonatomic, strong) CLLocation *currentLoc; -(void)getLocation; @end 

Lokation.m

 #import "Lokation.h" @implementation Lokation @synthesize locationManager = _locationManager; @synthesize currentLoc = _currentLoc; -(void)getLocation { NSLog(@"Inside Lokation::getLocation"); // active location determination self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; // We don't want to be notified of small changes in location, // preferring to use our last cached results, if any. self.locationManager.distanceFilter = 50; [self.locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"Inside Lokation::locationManager"); if (!oldLocation || (oldLocation.coordinate.latitude != newLocation.coordinate.latitude && oldLocation.coordinate.longitude != newLocation.coordinate.longitude)) { currentLat = newLocation.coordinate.latitude; currentLng = newLocation.coordinate.longitude; } else { // oldLocation currentLat = oldLocation.coordinate.latitude; currentLng = oldLocation.coordinate.longitude; } self.currentLoc = [[CLLocation alloc] initWithLatitude:currentLat longitude:currentLng]; NSLog(@"currentLat: %f currentLng %f", currentLat, currentLng); } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"%@", error); } @end 

问题是在getLocation运行之后meinelokation正在被解除分配,因为它只是一个局部variables。 创build一个强大的财产meineLokation,然后它应该正常工作。