gpt4 book ai didi

ios - CLLocationManager 不在 NSObject 中调用委托(delegate)

转载 作者:可可西里 更新时间:2023-11-01 04:16:10 26 4
gpt4 key购买 nike

我正在尝试创建一个辅助类,以便轻松地在任何其他类中获取手机的坐标。我遵循了一个教程,其中 UIViewController实现了 <CLLocationManagerDelegate>它奏效了。我试图在一个简单的 NSObject 中做同样的事情, 但后来我的委托(delegate)不再被召唤。

这是我的代码:

PS坐标.h

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

@interface PSCoordinates : NSObject <CLLocationManagerDelegate>

@property (nonatomic, retain) CLLocationManager* locationManager;


@end

PS坐标.m

#import "PSCoordinates.h"

@implementation PSCoordinates

- (id) init {
self = [super init];

if (self) {
self.locationManager = [[CLLocationManager alloc] init];
if ([CLLocationManager locationServicesEnabled])
{
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 100.0f;
NSLog(@"PSCoordinates init");
[self.locationManager startUpdatingLocation];
}
}
return self;
}

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Géolocalisation : %@",[newLocation description]);
}

- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Géolocalisation (erreur) : %@",[error description]);

}


@end

我是通过调用来调用它的

PSCoordinates * coordinates = [[PSCoordinates alloc] init];

按下按钮时。 init 正在工作,因为我可以看到 NSLog PSCoordinates init .

我发现其他主题的人有同样的问题,但没有一个答案解决了它。

非常感谢您的帮助。

最佳答案

使“PSCoordinates * coordinates”在你的类中成为全局的。它会起作用:)

关于ios - CLLocationManager 不在 NSObject 中调用委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16421695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com