locationManager startUpdatingLocation崩溃的应用程序

时间:2014-10-07 22:18:14

标签: ios objective-c core-location

这是我第一次开发应用程序。因此,当用户按下视图控制器中的“查看位置”按钮时,我希望显示用户的坐标和地址。

我编写了所有内容,当我按下模拟器中的按钮时,它突然关闭并指向IBAction按钮方法中的[newlocationManager startUpdatingLocation]代码。它说的类似“线程:1断点1.1”。它是什么意思,我该如何解决?

这是我的界面文件:

 @interface FirstViewController : UIViewController <CLLocationManagerDelegate>

 @property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
 @property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
 @property (weak, nonatomic) IBOutlet UILabel *addressLabel;

 - (IBAction)getCurrentLocation:(id)sender;

 @end

这是我的实施文件:

#import "FirstViewController.h"

@interface FirstViewController ()

@end

#pragma - UIViewController // Categorizes a group of methods

@implementation FirstViewController{
    //The CCLocationManager is the object that provides the location data and the methods.
    CLLocationManager * newlocationManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    newlocationManager = [[CLLocationManager alloc] init];
}

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

#pragma - IBAction // Categorizes a group of methods

- (IBAction)getCurrentLocation:(id)sender {
    newlocationManager.delegate = self;
    newlocationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [newlocationManager startUpdatingLocation];
}

#pragma - CLLocationManagerDelegate // Categorizes a group of methods

// Informs the delegate that it was unable to get location data. Displays error pop-up alert.
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    // The error why location could not be retreived
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    // Last object of the 'locations' array contains the most recent location. newLocation now points to the last object.
    CLLocation *newLocation = [locations lastObject];
    CLLocation *oldLocation;

    // Check the size of the NSArray (locations). If it contains a single object, just set oldLocation to nil.
    // If it contains more than 1 object then store that in oldLocation.

    if (locations.count>1){
        oldLocation = [locations objectAtIndex:(locations.count - 2)];
    }
    else
        oldLocation = nil;

    NSLog(@"didUpdateToLocation %@ from %@", newLocation, oldLocation);

    if (newLocation != nil){
    _longitudeLabel.text = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.longitude];
    _latitudeLabel.text = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.latitude];
    }
    //MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.0, 1500.0);
    //[regionsMapView setRegion:userLocation animated:YES];
}

@end

1 个答案:

答案 0 :(得分:0)

听起来像是在打破一个断点。可能没有任何错误。你看到这样的事吗? http://cl.ly/image/0T1G3n300G1r

Xcode Breakpoint indicator

如果是这样,只需单击并将其拖离排水沟即可。它会消失。下次你运行它不会打破那里。