سؤال

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.

هل كانت مفيدة؟

المحلول

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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top