【问题标题】:App runs fine without method, but I thought it required the method to function properly应用程序在没有方法的情况下运行良好,但我认为它需要方法才能正常运行
【发布时间】:2013-12-23 18:48:26
【问题描述】:

我有一个位置应用程序,过去几天在 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

【问题讨论】:

  • 想一想:在您的代码中,当用户点击按钮时,您会获得新的位置,而使用委托方法,您会在可用时立即获得新的位置。这就是区别。
  • 感谢您的回答。

标签: ios objective-c xcode5 cllocationmanager cllocation


【解决方案1】:

iOS 会检查您的班级是否能够接收以下数据:

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

因此,如果您的委托未实现该方法,则它不会尝试发送它。

【讨论】:

  • 感谢您的精彩回答!
猜你喜欢
  • 1970-01-01
  • 2018-04-18
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
相关资源
最近更新 更多