0

我有一个自定义导航栏,带有一个在每个视图中都可见的 locateButton

在此处输入图像描述

当用户点击 locateButton 时,我想在每个视图上更新 lat 和 long。我使用以下代码一次在一个视图中工作。

ViewController.m

- (UIBarButtonItem *)locateButton {
    [locationButton addTarget:self action:@selector(locationPressed:) forControlEvents:UIControlEventTouchUpInside];       
} 

- (IBAction)locationPressed:(id)sender {
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    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 didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        latLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        longLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }

    // Stop Location Manager
    [locationManager stopUpdatingLocation];

    // Reverse Geocoding
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            addressLabel.text = [NSString stringWithFormat:@"%@, %@",
                                 placemark.locality,
                                 placemark.administrativeArea];
            addressLabel.numberOfLines = 0;



        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

我正在尝试添加一个单例来完成此操作。

在位置.h

//  Location.h


#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>



@interface Location : NSObject <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager* locationManager;

+ (Location*)sharedSingleton;


@end

然后在 Location.M

//  Location.m


#import "Location.h"

@implementation Location

@synthesize locationManager;


- (id)init {
    self = [super init];

    if(self) {
        self.locationManager = [CLLocationManager new];
        [self.locationManager setDelegate:self];
        [self.locationManager setDistanceFilter:kCLDistanceFilterNone];
        [self.locationManager setHeadingFilter:kCLHeadingFilterNone];
        [self.locationManager startUpdatingLocation];
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        //do any more customization to your location manager
    }

    return self;
}

+ (Location*)sharedSingleton {
    static Location* sharedSingleton;
    if(!sharedSingleton) {
        @synchronized(sharedSingleton) {
            sharedSingleton = [Location new];
        }
    }

    return sharedSingleton;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    [locationManager stopUpdatingLocation];

}

@结尾

如何让我- (IBAction)locationPressed:(id)sender在另一个视图HomeViewController.m中调用这个 locationManager 方法并更新单例值?

4

1 回答 1

2

您可能会考虑将单例作为您的位置经理和位置经理代表。有一堆关于创建像这样的单例的stackoverflow问题:iOS 5中的单例?.

然后,当您的用户单击该按钮时,您的单例类将被调用并更新,其他视图将查看同一个单例以获取其位置信息。

于 2013-04-09T16:08:32.893 回答