user1010563
user1010563

Reputation:

Set delegate of CLLocationManager (Two different ways, are they equal?)

Working on a tutorial with CLLocationManager where I set the delegate within the init method:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) 
    {
        locationManager = [[CLLocationManager alloc] init];
        [locationManager setDelegate:self];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [locationManager startUpdatingLocation];
    }
    return self;
}

Set the delegate:

[locationManager setDelegate:self];

In another tutorial I set the delegate in a header file:

@interface MyViewController : UIViewController <CLLocationManagerDelegate>

Are they equal? Whats the difference (if there is one)?

Upvotes: 0

Views: 1875

Answers (2)

sofokli1
sofokli1

Reputation: 36

You will need both of them if you want to implement and get callbacks from the delegate of CLLocationManger

You specify this in the header

@interface MyViewController : UIViewController <CLLocationManagerDelegate>

This tells xcode that MyViewController will implement the CLLocationManagerDelegate methods. If there are non-optional delegate methods, xcode will remind you to implement them.

With the line below

[locationManager setDelegate:self];

You tell your instance of CLLocationManager (locationManager) that MyViewController (self) will be the delegate and it should call all the implemented CLLocationManagerDelegate methods that you have implemented (if needed)

Upvotes: 1

unexpectedvalue
unexpectedvalue

Reputation: 6139

Those are 2 different things.

CLLocationManagerDelegate is a protocol.
[locationManager setDelegate:self] just sets the objects delegate, so CLLocationManager can call the implemented delegate methods. For this to work correctly, self must conform to the CLLocationManagerDelegate protocol.

In other words, you need to do both.

Upvotes: 2

Related Questions