简体   繁体   English

将ViewController连接到委托类-CLLocationManager / iOS8

[英]Connecting a viewcontroller to a delegate class - CLLocationManager / iOS8

While trying to link my view controller to a delegate class (that implements CLLocation call backs), I am getting an error when initializing my delegate. 尝试将我的视图控制器链接到委托类(实现CLLocation回调)时,初始化我的委托时遇到错误。

My VC.h: 我的VC.h:

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

Instantiating MyLocationController in the main VC.m: 在主VC.m中实例化MyLocationController:

CLController = [[MyLocationController alloc] init]; CLController = [[[MyLocationController alloc] init]; CLController.locationManager.delegate = self; CLController.locationManager.delegate =自我; [CLController.locationManager requestWhenInUseAuthorization]; [CLController.locationManager requestWhenInUseAuthorization]; CLController.locationManager.distanceFilter = kCLDistanceFilterNone; CLController.locationManager.distanceFilter = kCLDistanceFilterNone; CLController.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; CLController.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; [CLController.locationManager startUpdatingLocation]; [CLController.locationManager startUpdatingLocation];

The class header file: 类头文件:

#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

and .m file (where I get the error): 和.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;
}

I noticed that this error does not show up in previous iOS versions. 我注意到此错误在以前的iOS版本中没有出现。

Thanks for the help. 谢谢您的帮助。

According to the documentation , MyLocationController must conform to CLLocationManagerDelegate . 根据文档MyLocationController必须符合CLLocationManagerDelegate

@interface MyLocationController: NSObject <CLLocationManagerDelegate>

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

@end

As a side note, you don't need to declare the iVars for the properties locationManager and delegate . 附带说明一下,您不需要为属性locationManagerdelegate声明iVars。 The iVars _locationManager and _delegate will be auto generated for you. iVars _locationManager_delegate将自动为您生成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM