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

时间:2015-03-22 00:01:15

标签: ios objective-c iphone uiviewcontroller delegates

在尝试将我的视图控制器链接到委托类(实现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版本中没有显示此错误。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

根据the documentationMyLocationController必须符合CLLocationManagerDelegate

@interface MyLocationController: NSObject <CLLocationManagerDelegate>

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

@end

作为旁注,您无需为属性locationManagerdelegate声明iVar。将自动为您生成iVars _locationManager_delegate