【问题标题】:objective-c locationManager as delegate, be notified if location changed作为委托的objective-c locationManager,如果位置更改,则通知
【发布时间】:2013-07-02 07:40:10
【问题描述】:

我在 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);
}

【问题讨论】:

  • 试试[self.delegate performSelector:@selector(didFindLocation)];?还有NSLogdelegate的值来检查它是否是nil。此外,当您声明 @property 时,无需显式创建 ivar。它将自动为您创建。
  • 顶部。那成功了。现在它正在工作。谢谢。

标签: ios objective-c delegates cllocationmanager


【解决方案1】:

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

删除__unsafe_unretained id&lt;LokationDelegate&gt; delegate; 并用@property (retain) id&lt;LokationDelegate&gt; delegate 替换@property (assign, nonatomic) id&lt;LokationDelegate&gt; delegate(并合成属性),它对我来说就像魅力

【讨论】:

  • 做到了。 @property (assign, nonatomic) id &lt;LokationDelegate&gt; delegate 工作正常。至少在 xcode 4.6.2 中不再需要 Snythesize 属性。 @property (retain) ... 有什么优势?
  • @MuraliK 保留委托是个坏主意,因为它会导致代码中的retain cycles 导致内存泄漏。
猜你喜欢
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多