2013-12-23 35 views
0

我有一個位置的應用程序,在ViewController.m文件,在過去幾天的頭有以下方法:應用程序運行,而無需法測得的,但我認爲它需要的方法才能正常

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

但是,我剛剛刪除了上面的代碼,該應用程序仍然100%正常運行。我不明白當xcode文檔清楚地說明這種方法的目的是「告訴代理何時有新的位置數據」時,這是如何實現的。

我能想到的唯一的事情是,它說:「位置數據」和上面的方法已經建立在我進口CoreLocation.h文件,因此已經可以爲我用,並已存儲數據。

只是想在我繼續之前確定理解背後的所有理論。

感謝您幫助清理此問題。

這裏是我的整個ViewController.m代碼(該方法還包括):

#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 
+0

想一想:在你的代碼中,當用戶點擊按鈕時,你會得到新的位置,使用委託方法,你可以立即獲得新的位置。這是不同的。 –

+0

謝謝你的回答。 – user3117509

回答

3

的iOS檢查類是能夠與像接收數據:

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

所以如果你的委託沒有實現該方法,那麼它不會嘗試發送它。

+0

謝謝你的偉大答案! – user3117509

相關問題