1
votes

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.

My VC.h:

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

Instantiating MyLocationController in the main VC.m:

CLController = [[MyLocationController alloc] init]; CLController.locationManager.delegate = self; [CLController.locationManager requestWhenInUseAuthorization]; CLController.locationManager.distanceFilter = kCLDistanceFilterNone; CLController.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; [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):

- (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.

Thanks for the help.

1
Thanks for the edits @jeffery! No idea how to do that yet..TommyG
Your protocol declaration is empty. Where is your MyLocationControllerDelegate defined?Duncan C

1 Answers

1
votes

According to the documentation, MyLocationController must conform to 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. The iVars _locationManager and _delegate will be auto generated for you.