NSString 从另一个类访问时返回 (null)



我从另一个类访问NSString时遇到问题,因为它总是以(null)返回。目前我有两个视图控制器,第一个有用户坐标,第二个有一个MKMapView,用图钉显示当前用户在地图上的位置。问题是我希望引脚在他的副标题中显示用户位置的坐标,但latitudeString的值总是(null),但只在另一ViewController上,因为在第一个latitudeString正确显示纬度值。

代码如下:

ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
}
@property (strong, nonatomic) NSString                          *latitudeString;
@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()
{
}
@property (strong, nonatomic) CLLocationManager                 *locationManager;
@end
@implementation ViewController
{
CLLocation          *newLocation;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
newLocation = [locations lastObject];
self.latitudeString = [NSString stringWithFormat:@"%gu00B0", newLocation.coordinate.latitude];
}
@end

MapViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController : UIViewController <MKMapViewDelegate>
{
ViewController              *firstViewController;
}
@property (strong, nonatomic) ViewController                *firstViewController;
@property (strong, nonatomic) IBOutlet MKMapView            *mapView;
@end

MapViewController.m

#import "MapViewController.h"
#import "ViewController.h"
@interface MapViewController ()
{
}
@end
@implementation MapViewController
- (void)viewDidLoad
{
self.mapView.showsUserLocation = YES;
self.mapView.delegate = self;
self.mapView.mapType = MKMapTypeHybrid;
self.mapView.userLocation.title = @"Posizione attuale";
firstViewController = [[ViewController alloc] init];
self.mapView.userLocation.subtitle = [NSString stringWithFormat:@"%@", firstViewController.latitudeString]; // Nil
[super viewDidLoad];
}
@end

有人可以帮助我解决这个问题吗?谢谢。

这是因为您在locationManager: didUpdateLocations:方法调用中设置了latitudeString的值,但是在MapViewController中,自从创建firstViewController以来,您没有调用过该方法。

仅在调用时设置 latitudeString 的值

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

如果在创建 firstViewController 实例后不调用该方法,latitudeString 肯定会指向 nil。

此方法是来自位置管理器的回调。尝试将 init 覆盖到 firstViewController 的实现中,如下所示:

- (instancetype)init
{
self = [super init];
if (self){
self.locationManager = [CLLocationManager init];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
return self;
}

并使您的第一个ViewController实现协议"CLLocationManagerDelegate"。

这样,您应该能够启动 locationManager,并且应该自动调用委托方法。一旦调用该方法,NSString 就会按预期进行设置。

但是我不明白为什么要以这种方式创建新的 VC 对象?你打算稍后在屏幕上展示吗?

就我个人而言,我建议使用一个单独的类来管理实现单例模式的 CLLocationManager - 以便您可以从应用程序中的任何位置访问相同的 CLLocationManager 实例。 希望这有帮助。

您可以做一件事,即在文件中声明一个 NSString 变量.pch以便全局声明并且可以从任何类访问。 然后在ViewController.m中,您更改以下内容:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
newLocation = [locations lastObject];
self.latitudeString = [NSString stringWithFormat:@"%gu00B0", newLocation.coordinate.latitude];
string_declare_in_pch=self.latitudeString; // new line
}

在MapViewController.m

- (void)viewDidLoad {
self.mapView.userLocation.subtitle = [NSString stringWithFormat:@"%@", string_declare_in_pch]; // new 
}

我希望它对你有用。 如果您需要任何澄清,请告诉我。

最新更新