无法访问另一个类的NSString

时间:2014-01-21 18:59:49

标签: ios objective-c class nsstring

从另一个类中获取名为NSString的{​​{1}}的值时遇到问题。目前我有两个视图控制器,第一个具有用户坐标,第二个具有latitudeString,用于显示地图上的当前用户位置。问题是我希望引脚在他的副标题中显示用户位置的坐标,但我无法获得MKMapView的值,Xcode给了我这个错误:

latitudeString

以下是代码:

ViewController.h

Property 'latitudeString' not found on object of type 'ViewController *'

ViewController.m

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

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{

}

@end

MapViewController.h

#import "ViewController.h"

@interface ViewController ()
{
    CLLocation          *newLocation;
    NSString            *latitudeString;
}

@property (strong, nonatomic) CLLocationManager                 *locationManager;
@property (strong, nonatomic) NSString                          *latitudeString;

@end

@implementation ViewController

@synthesize latitudeString;

CLLocationManager *locationManager;

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    newLocation = [locations lastObject];

    latitudeString = [NSString stringWithFormat:@"%g\u00B0", newLocation.coordinate.latitude];
}

@end

MapViewController.m

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

@interface MapViewController : UIViewController <MKMapViewDelegate>
{

}

@end

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

更新#1

ViewController.h

#import "MapViewController.h"
#import "ViewController.h"

@interface MapViewController ()
{
    ViewController              *firstViewController;
}

@property (strong, nonatomic) ViewController                *firstViewController;
@property (strong, nonatomic) IBOutlet MKMapView            *mapView;

@end

@implementation MapViewController

@synthesize firstViewController;

- (void)viewDidLoad
{
    self.mapView.showsUserLocation = YES;
    self.mapView.delegate = self;
    self.mapView.userLocation.title = @"Posizione attuale";
    self.mapView.userLocation.subtitle = [NSString stringWithFormat:@"%@", firstViewController.latitudeString]; // ERROR

    [super viewDidLoad];
}

@end

ViewController.m

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

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
    CLLocation          *newLocation;
    NSString            *latitudeString;
}

@end

MapViewController.h

#import "ViewController.h"

@interface ViewController ()
{
}

@property (strong, nonatomic) CLLocationManager                 *locationManager;

@end

@implementation ViewController

CLLocationManager *locationManager;

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    newLocation = [locations lastObject];

    self->latitudeString = [NSString stringWithFormat:@"%g\u00B0", newLocation.coordinate.latitude];
}

@end

MapViewController.m

#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

1 个答案:

答案 0 :(得分:4)

latitudeString的{​​{1}}属性是私有财产。只有ViewController的方法才能访问它。如果您希望它是公共属性,则需要将属性声明移动到.h文件。

BTW - 摆脱对ViewController的所有电话。使用时,它们已经过时了。还要摆脱为属性定义的ivars。它们也已经过时了。

试试这个:

<强> ViewController.h

@synthesize

<强> ViewController.m

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

@interface ViewController : UIViewController <CLLocationManagerDelegate>

// A public property
@property (strong, nonatomic) NSString *latitudeString;

@end