我如何使用在另一个类中找到的位置?

时间:2013-05-08 17:48:34

标签: xcode class null location nslog

我正在使用此代码获取用户的位置:

@implementation TabBar
CLLocationManager *locationManager;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    // NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

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

        NSLog(@"Latitude:%@,Longitude:%@",UsersCurrentLatitude,UsersCurrentLongitude);

    }

并在nslog屏幕中成功显示位置。但是当我想在导入我的位置类的另一个类中使用那些位置时(在这种情况下它是TabBar),location变为null。我该怎么做才能解决这个问题? 编辑:

在我为我的问题尝试两种不同的方法之后,我仍然在NsLog上得到null:

第一种方法:

在AppDelegate.h:

@property(nonatomic,retain)NSString *latitude;
@property(nonatomic,retain)NSString 
At AppDelegate.mlongitude;
@synthesize latitude;
@synthesize longitude;

在TabBar.m

#import "AppDelegate.h"

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
    if (currentLocation != nil) {
        [(AppDelegate *)[[UIApplication sharedApplication] delegate] setLatitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]];

        [(AppDelegate *)[[UIApplication sharedApplication] delegate] setLongitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]];
}  
    }

At my SecondView.m
#import "AppDelegate.h"
(void)viewDidLoad
{
    [super viewDidLoad];

    NSString *UsersCurrentLatitude = [(AppDelegate *)[[UIApplication sharedApplication] delegate] latitude];
    NSString *UsersCurrentLongitude = [(AppDelegate *)[[UIApplication sharedApplication] delegate] longitude];

NSLog(@"latitude:%@,longitude%@",UsersCurrentLatitude,UsersCurrentLongitude);
}

第二种方法(我在youtube中找到了一个教程并为我的问题安排了这个方法,所以它可能是无意义的代码):

我创建了一个名为SingletonClass的NSObject类 在SingletonClass.h

#import <CoreLocation/CoreLocation.h>

@interface SingletonClass : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) NSNumber *UsersCurrentLatitude;
@property (nonatomic, strong) NSNumber *UsersCurrentLongitude;

+(SingletonClass *) Lokasyon;

@end

在SingletonClass.m

#import "SingletonClass.h"

@implementation SingletonClass {
    CLLocationManager *locationManager;

}
@synthesize UsersCurrentLongitude;
@synthesize UsersCurrentLatitude;

+(SingletonClass *)Lokasyon {
    static SingletonClass *Lokasyon = nil;
    if (!Lokasyon) {
        Lokasyon = [[super allocWithZone:nil] init];
        }
    return Lokasyon;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
      UsersCurrentLongitude = [NSNumber numberWithFloat:currentLocation.coordinate.longitude];
        UsersCurrentLatitude = [NSNumber numberWithFloat:currentLocation.coordinate.latitude];

    }
}
@end

在我的SeconViewController

#import "SingletonClass.h"
- (void)viewDidLoad
{
    [super viewDidLoad];

NSNumber *UsersCurrentLongitude1 = [[SingletonClass Lokasyon]UsersCurrentLongitude];
    NSLog(@"%@",UsersCurrentLongitude1);
}

1 个答案:

答案 0 :(得分:0)

Appdelegate.h中声明它们 -

    @property(nonatomic,retain)NSString *latitude;
    @property(nonatomic,retain)NSString *longitude;

请确保您在latitude课程中合成longitudeAppDelegate.m属性。

TabBar.m中设置CLLocationManager的{​​{1}}

中的位置
didUpdateToLocation:

访问其他类,如 -

if (currentLocation != nil) {
    [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setLatitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]];

    [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setLongitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]];

        //UsersCurrentLatitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];

        //NSLog(@"Latitude:%@,Longitude:%@",UsersCurrentLatitude,UsersCurrentLongitude);

    }
PS:还有许多其他方法可以满足您的需求,例如:拥有 NSString aLatitude = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] latitude]; NSString aLongitude = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] longitude]; 课程或在iOS应用Singleton中保存,即在应用File system ..