[英]Connecting a viewcontroller to a delegate class - CLLocationManager / iOS8
嘗試將我的視圖控制器鏈接到委托類(實現CLLocation回調)時,初始化我的委托時遇到錯誤。
我的VC.h:
@interface ViewController : UIViewController <MyLocationControllerDelegate> {
MyLocationController *CLController;
}
在主VC.m中實例化MyLocationController:
CLController = [[[MyLocationController alloc] init]; CLController.locationManager.delegate =自我; [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版本中沒有出現。
謝謝您的幫助。
根據文檔 , MyLocationController
必須符合CLLocationManagerDelegate
。
@interface MyLocationController: NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, weak) id delegate;
@end
附帶說明一下,您不需要為屬性locationManager
和delegate
聲明iVars。 iVars _locationManager
和_delegate
將自動為您生成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.