gpt4 book ai didi

ios - CLLocation 的 NSLog 在启动时不打印,仅在按下按钮时打印

转载 作者:行者123 更新时间:2023-11-28 20:09:54 26 4
gpt4 key购买 nike

我的应用在按下按钮时在文本标签中显示用户的最后已知/当前坐标。

我在我的主文件中设置了一个代码块来将用户的纬度坐标打印到日志中,但是当我运行该应用程序时它没有将任何内容打印到日志中。

为什么 NSLog 不打印到控制台?

这是一段代码,一旦应用启动并且用户允许应用访问他们的位置,它应该将位置打印到日志中:

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

CLLocation * currentLocation = [locations lastObject];

NSLog(@"%f", currentLocation.coordinate.latitude);

下面是我完整的 ViewController.h 代码:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>


@interface ViewController : UIViewController <CLLocationManagerDelegate>

@property (nonatomic, strong) IBOutlet UILabel * gpsLabel;
@property (nonatomic, strong) CLLocationManager * gpsLM;

-(IBAction)gpsButton;

@end

这是我完整的 ViewController.m 代码:​​

#import "ViewController.h" //This imports the all of the code we have typed in the     ViewController.h file.
#import <CoreLocation/CoreLocation.h> //This imports the CoreLocation framework needed for location apps.


//This assigns the Location Manager's delegate to this view controller

@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];

//This allocates memory for and initializes the gpsLM object we setup in ViewController.h
//This means that we can now use the object and do things with it.

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

//This calls a startUpdatingLocation method for our CLLocationManager object called gpsLM.
//Because this is all in viewDidLoad, it all gets executed right away as soon as the app is opened.

[self.gpsLM startUpdatingLocation];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


//This executes the instance method that we declared above in the header.
//Now we are actually implementing the method and can tell it what we want it to do.

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

//This creates an object called currentLocation and sets it's value to whatever the last value is in the locations array.
//Notice how it is also calling a method of lastObject for the object called locations.
//So remember that you can set variables and objects equal to the result of a method call.

CLLocation * currentLocation = [locations lastObject];

//This prints out text to the debug console that states the latitude coordinate of the user's iPhone.

NSLog(@"%f", currentLocation.coordinate.latitude);

}


-(IBAction)gpsButton{

CLLocation * currentLocation = self.gpsLM.location;

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

}

@end

最佳答案

您似乎忘记了分配位置管理器委托(delegate):

self.gpsLM = [[CLLocationManager alloc]init];
self.gpsLM.delegate = self; // <-- ADD THIS
[self.gpsLM startUpdatingLocation];

如果没有这个分配,位置管理器就不知道要将位置更新提供给哪个对象。 locationManager:didUpdateLocations: 方法永远不会运行。

关于ios - CLLocation 的 NSLog 在启动时不打印,仅在按下按钮时打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20722564/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com