objective-c locationManager作为委托,如果位置发生变化,则会收到通知

时间:2013-07-04 10:26:34

标签: ios objective-c delegates cllocationmanager

我在own class and file

中找到了我的locationManager

现在我希望在更新位置时收到通知。所以我尝试实现委托模式。但由于某种原因,它不起作用。我做了什么:

  • 在Location.h中:指定的类,协议,ivar和属性id,委托方法
  • 在Location.m中:添加了对委派方法的调用
  • 在ViewController.h中:添加了委托协议
  • 在ViewController.m中:实现了委托方法

构建并运行代码。但是没有调用ViewController.m中的委托方法:( 我错过了什么想法?

输出

2013-07-04 11:55:30.429 Sandbox2[2001:c07] Inside Lokation::getLocation
2013-07-04 11:55:30.443 Sandbox2[2001:c07] Inside Lokation::locationManager
2013-07-04 11:55:30.449 Sandbox2[2001:c07] currentLat: 51.509980 currentLng -0.133700
// Missing NSLog Statement of the delegate method didFindLocation...

ViewController.h

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
// added Lokation header
#import "Lokation.h"

// added LokationDelegate
@interface ViewController : UIViewController <CLLocationManagerDelegate, LokationDelegate>

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) Lokation *lokation; 
@end

@implementation ViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    //[self getLocation];
    self.lokation = [[Lokation alloc] init];
    self.lokation.delegate = self;
    [self.lokation getLocation];
}

// added delegate function of Lokation
// Should be run after the locationmanager found a location
-(void)didFindLocation {
    NSLog(@"I am found a lokation and I am now in the ViewController");
}

Lokation.h(20130704更新为工作代码)

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

// Added class, protocol and method for the delegate
@class Lokation;
@protocol LokationDelegate <NSObject>

-(void)didFindLocation;

@end

@interface Lokation : NSObject <CLLocationManagerDelegate> {
    CLLocationDegrees currentLat;
    CLLocationDegrees currentLng;
}

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation *currentLoc;
// added delegate property
@property (assign, nonatomic) id<LokationDelegate> delegate; 

-(void)getLocation; 

@end

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

#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);

    // added call to the delegate function
    [self.delegate performSelector:@selector(didFindLocation)];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    NSLog(@"%@", error);
}

1 个答案:

答案 0 :(得分:0)

嗯......我认为你做得对,但如果我做的改变如下:

删除__unsafe_unretained id<LokationDelegate> delegate;并将@property (assign, nonatomic) id<LokationDelegate> delegate替换为@property (retain) id<LokationDelegate> delegate(并合成属性),它对我来说就像魅力一样