在尝试将我的视图控制器链接到委托类(实现CLLocation回调)时,我在初始化委托时遇到错误。
我的VC.h:
@interface ViewController : UIViewController <MyLocationControllerDelegate> {
MyLocationController *CLController;
}
在主VC.m中实例化MyLocationController:
CLController = [[MyLocationController alloc] init]; 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版本中没有显示此错误。
感谢您的帮助。
答案 0 :(得分:1)
根据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
。