在尝试将我的 View Controller 链接到委托(delegate)类(实现 CLLocation 回调)时,我在初始化委托(delegate)时遇到错误。
我的 VC.h:
@interface ViewController : UIViewController <MyLocationControllerDelegate> {
MyLocationController *CLController;
}
在主 VC.m 中实例化 MyLocationController:
CLController = [[MyLocationController 分配] 初始化]; CLController.locationManager.delegate = self; [CLController.locationManager requestWhenInUseAuthorization]; CLController.locationManager.distanceFilter = kCLDistanceFilterNone; CLController.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; [CLController.locationManager startUpdatingLocation];
类头文件:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@protocol MyLocationControllerDelegate;
@interface MyLocationController: NSObject {
CLLocationManager *locationManager;
__weak id delegate;
}
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, weak) id delegate;
@end
和 .m 文件(我收到错误的地方):
- (id)init
{
self = [super init];
if(self != nil) {
self.locationManager = [[CLLocationManager alloc] init]; // Create new instance of locationManager
self.locationManager.delegate = self; // Set the delegate as self.
}
return self;
}
我注意到这个错误不会出现在以前的 iOS 版本中。
感谢您的帮助。
最佳答案
根据 the documentation , MyLocationController
必须符合 CLLocationManagerDelegate
。
@interface MyLocationController: NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, weak) id delegate;
@end
附带说明一下,您不需要为属性 locationManager
和 delegate
声明 iVar。 iVars _locationManager
和 _delegate
将为您自动生成。
关于ios - 将 View Controller 连接到委托(delegate)类 - CLLocationManager/iOS8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189452/