将 viewcontroller 连接到委托 class - CLLocationManager / iOS8

Connecting a viewcontroller to a delegate class - CLLocationManager / iOS8

在尝试 link 我的视图控制器到委托 class(实现 CLLocation 回调)时,我在初始化委托时遇到错误。

我的VC.h:

@interface ViewController : UIViewController <MyLocationControllerDelegate> {
  MyLocationController *CLController;
}

在主实例中实例化 MyLocationController VC.m:

CLController = [[MyLocationController 分配] 初始化]; CLController.locationManager.delegate = 自我; [CLController.locationManager requestWhenInUseAuthorization]; CLController.locationManager.distanceFilter = kCLDistanceFilterNone; CLController.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; [CLController.locationManager 开始更新位置];

class头文件:

#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 documentationMyLocationController必须符合CLLocationManagerDelegate

@interface MyLocationController: NSObject <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, weak) id delegate;

@end

附带说明一下,您不需要为属性 locationManagerdelegate 声明 iVar。 iVars _locationManager_delegate 将自动为您生成。