我有一个位置应用程序,在过去几天的 ViewController.m 文件的 header 中有以下方法:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
但是,我只是从我的代码中删除了上面的内容,应用程序仍然可以 100% 正常运行。当 xcode 文档清楚地说明此方法的目的是“在新位置数据可用时告诉委托(delegate)人”时,我不明白这是怎么可能的。
我唯一能想到的是它说“new 位置数据”并且上面的方法已经在我导入的 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
最佳答案
iOS 检查您的类是否能够接收数据,例如:
if ( [delegate respondsToSelector:@selector(didUpdateLocations:)] ){
[delegate performSelector:@selector(didUpdateLocations:) withArgs:....]
}
因此,如果您的委托(delegate)未实现该方法,则它不会尝试发送它。
关于ios - 应用程序在没有方法的情况下运行良好,但我认为它需要方法才能正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20749440/