Frage

I have a location app that had the following method in the header of the ViewController.m file for the past couple of days:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

However, I just deleted the above out of my code and the app still runs 100% fine. I don't understand how this is possible when the xcode documentation clearly says that the purpose of this method is to "tell the delegate when new location data is available."

The only thing I can think of is that it says "new location data" and that the above method is already setup in the CoreLocation.h file that I imported, and therefore already available for my use and has already stored the data.

Just want to make sure I understand the theory behind all of this before I move on.

Thank you for the help in clearing this up.

Here is my entire ViewController.m code(with the method still included):

#import "ViewController.h" 
#import <CoreLocation/CoreLocation.h> 

@interface ViewController () <CLLocationManagerDelegate>

//This tells the delegate that new location data is available. Manager is the object that updates the event, and the locations object is where the array of location data is stored.

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

@end

@implementation ViewController

- (void)viewDidLoad
{

[super viewDidLoad];

self.gpsLM = [[CLLocationManager alloc]init];

NSLog(@"Location services enabled: %u",[CLLocationManager authorizationStatus]);


[self.gpsLM startUpdatingLocation];

self.gpsLM.delegate = self;

CLLocation * currentLocation = self.gpsLM.location;
NSLog(@"Your current location is: %@", currentLocation);    

}

- (void)didReceiveMemoryWarning

{
[super didReceiveMemoryWarning];
}

-(IBAction)gpsButton{

CLLocation * currentLocation = self.gpsLM.location;

self.gpsLabel.text = [NSString stringWithFormat:@"Your Location is %@", currentLocation];

NSLog(@"Location services enabled: %u",[CLLocationManager authorizationStatus]);

NSLog(@"Your current location is: %@", currentLocation);

}
@end
War es hilfreich?

Lösung

iOS checks if your class is capable of receiving the data with something like:

if ( [delegate respondsToSelector:@selector(didUpdateLocations:)] ){
    [delegate performSelector:@selector(didUpdateLocations:) withArgs:....]
}

So if your delegate doesn't implement the method, then it doesn't attempt to send it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top