-3

In my app, I am trying to access a variable that is set in one class (ClassC), and then pops off 2 views to ClassA, and that view (ClassA), needs to access that variable set in ClassC. Let's see if I can make this a little clearer:

A button in my ClassC calls this method:

-(IBAction) mapItBtn:(id)sender
{
    MapAnnotation* target = [[MapAnnotation alloc] initWithTitle:building.Name subTitle:@"" Coordinate:CLLocationCoordinate2DMake(building.Latitude, building.Longitude)];

    DIRlat = building.Latitude;
    DIRlon = building.Longitude;

    [mapUI removeAnnotations:mapUI.annotations];
    [mapUI addAnnotation:target];
    [mapUI selectAnnotation:target animated:YES];
    [[mapUI viewForAnnotation:target] setCanShowCallout:YES];

    MKCoordinateRegion region;
    region.center=target.coordinate;
    region.span.latitudeDelta=mapUI.region.span.latitudeDelta;
    region.span.longitudeDelta=mapUI.region.span.longitudeDelta;

    [mapUI setRegion:region animated:TRUE];

    [mapUI setHidden:NO];

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:(self.navigationController.viewControllers.count - 3)] animated:YES];
 }

Once the last line is completed and the app popsToViewController:...objectAtIndex:...-3, that view (ClassA) needs to access the values of DIRlat and DIRlon in ClassC. The class importing structure is ClassA imports ClassB which imports ClassC.

Any suggestions on how to best accomplish this task would be immensely appreciated!

Thanks!

EDIT: Things I have tried, and have been unsuccessful:

Creating a variable in ClassA, and then in ClassC I have @implementation ClassA and @synthesize AVariable, however this led to a Parse Issue where ClassA resulted in reading ClassB as an unknown type.

The opposite actions of the above code, as in creating a variable in ClassC, and then @implementation ClassC in ClassA. This resulted in a circular dependency (Mach-O linker error).

The thing that I am currently trying to accomplish is to make a singleton class, however since I have had no experience with it, progress is going slowly.

VonKoob
  • 77
  • 1
  • 9
  • There are dozens of questions here on this subject. Do some looking around, starting with the sidebar -----> – jscs Aug 01 '13 at 19:47
  • That is what I have been doing over the last few hours, I have tried making a public variable, various ways of `@implementation ClassA` in `ClassC` as well as `@implementation ClassC` in `ClassA`. Thank you for your suggestion, I'll continue going through previous S.O. topics. – VonKoob Aug 01 '13 at 19:51
  • Please be more explicit about what you tried, why you thought that would work, and what goes wrong. – jscs Aug 01 '13 at 20:10
  • 1
    possible duplicate of [How can I access variables from another class?](http://stackoverflow.com/questions/658697/how-can-i-access-variables-from-another-class) – Chuck Aug 01 '13 at 20:17
  • Okay guys thanks for letting me know that there are other S.O. topics on this, I know that there are other questions similar to this one. I have gone through many of them and tried their suggestions as best as I can as it relates to my code. I have also edited my question to include all of the things I have tried so far. – VonKoob Aug 01 '13 at 20:25

1 Answers1

0

Answer: Solved my problem by creating a Singleton class by following a guide found here.

Edit: Showing the code solution:

MKLocationManagerSingleton.h

#import <Foundation/Foundation.h>

@interface MKLocationManagerSingleton : NSObject {
    double MKlat;
    double MKlon;
}
@property (nonatomic, readwrite) double MKlat;
@property (nonatomic, readwrite) double MKlon;
+ (id)sharedManager;
@end

MKLocationManagerSingleton.m

#import "MKLocationManagerSingleton.h"
@implementation MKLocationManagerSingleton
@synthesize MKlat, MKlon;
#pragma mark Singleton Methods

+ (id)sharedManager {
    static MKLocationManagerSingleton *sharedMKLocationManagerSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    sharedMKLocationManagerSingleton = [[self alloc] init];
    });
    return sharedMKLocationManagerSingleton;
}

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

- (void)dealloc {   
}
@end

And requested the values for MKlat and MKlon with:

MKLocationManager *sharedManager = [MKLocationManager sharedManager];
destination.latitude = sharedManager.MKlat;
destination.longitude = sharedManager.MKlon;
VonKoob
  • 77
  • 1
  • 9