自己的类中的objective-c源locationManager

Posted

技术标签:

【中文标题】自己的类中的objective-c源locationManager【英文标题】:objective-c source locationManager out in own class 【发布时间】:2013-07-01 06:10:15 【问题描述】:

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

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

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

输出

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

缺少什么以及如何整合它?有些东西告诉我“代表”,但我是这个话题的菜鸟。

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

定位.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

定位.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

【问题讨论】:

【参考方案1】:

问题是 meinelokation 在 getLocation 运行后立即被释放,因为它只是一个局部变量。创建一个强大的属性 meineLokation,然后它应该可以正常工作。

【讨论】:

没有用。添加了@property (strong, nonatomic) Lokation *lokation; 并在viewDidLoadLokation *lokation = [[Lokation alloc] init];[lokation getLocation]; 中使用它。仅调用 getLocation 的结果相同。 @jerik,那是因为你做得不对。应该是,self.lokation = [[Lokation alloc] init],而不是 Lokation *lokation = [[Lokation alloc] init] 伙计,我很愚蠢。你说的太对了。现在按预期工作。谢谢。我只见树木不见森林......

以上是关于自己的类中的objective-c源locationManager的主要内容,如果未能解决你的问题,请参考以下文章

objective-c 中如何在一个函数中调用自己类中的另外一个函数

事件处理模式

将实现协议的 Swift 类导入到 Objective-C 类中

使用原始数组/指针参数验证 Objective-C 中的方法调用

Objective-C - 从不同的类访问属性[重复]

当我们在 swift 类中使用 Objective-c 属性时的桥接问题