ios - 将 View Controller 连接到委托(delegate)类 - CLLocationManager/iOS8

标签 ios objective-c iphone uiviewcontroller delegates

在尝试将我的 View Controller 链接到委托(delegate)类(实现 CLLocation 回调)时,我在初始化委托(delegate)时遇到错误。

我的 VC.h:

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

在主 VC.m 中实例化 MyLocationController:

CLController = [[MyLocationController 分配] 初始化]; 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 版本中。

感谢您的帮助。

最佳答案

根据 the documentation , MyLocationController 必须符合 CLLocationManagerDelegate

@interface MyLocationController: NSObject <CLLocationManagerDelegate>

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

@end

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

关于ios - 将 View Controller 连接到委托(delegate)类 - CLLocationManager/iOS8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29189452/

相关文章:

android - 无法使用原生代码在 V2 Flutter 中截屏,它在 V1 Flutter 中运行良好

ios - 使用核心图根据圆半径将散点图缩放到 Y 轴

iphone - 在 iPhone 上监听 Co​​coaASyncSocket 接收连接,但新套接字不调用委托(delegate)

ios - 应用程序安装失败 : App has disallowed Info. plist key

iphone - UITableView 中的编辑模式

iphone - 苹果的MFi计划

ios - 实例和类方法,ObjC 接口(interface)

ios - 无法加载用 swift 制作的 pod

objective-c - 关于Objective-C中的 getter 和 setter

ios - 如何在点击 UITextField 时在 UIPickerView 中显示信息