xcode - how can i use location which is found in another class? -
i taking user's location code:
@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); } and in nslog screen showing location successfully. when want use locations in class imported location class (in case tabbar), location turns null. can solve issue? edited:
after try 2 different method issue still getting null @ nslog:
first method:
at appdelegate.h:
@property(nonatomic,retain)nsstring *latitude; @property(nonatomic,retain)nsstring @ appdelegate.mlongitude; @synthesize latitude; @synthesize longitude; at tabbar.m
#import "appdelegate.h" - (void)viewdidload { [super viewdidload]; // additional setup after loading 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]]; } } @ 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); } second method (i found tutorial in youtube , arrange method issue, can meaningless code):
i made nsobject class named singletonclass @ singletonclass.h
#import <corelocation/corelocation.h> @interface singletonclass : nsobject <cllocationmanagerdelegate> @property (nonatomic, strong) nsnumber *userscurrentlatitude; @property (nonatomic, strong) nsnumber *userscurrentlongitude; +(singletonclass *) lokasyon; @end at 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 at seconviewcontroller
#import "singletonclass.h" - (void)viewdidload { [super viewdidload]; nsnumber *userscurrentlongitude1 = [[singletonclass lokasyon]userscurrentlongitude]; nslog(@"%@",userscurrentlongitude1); }
declare them in appdelegate.h like-
@property(nonatomic,retain)nsstring *latitude; @property(nonatomic,retain)nsstring *longitude; make sure u, synthesize latitude , longitude properties in appdelegate.m class.
set them in tabbar.m u location in cllocationmanager's 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); } access in other class(es) like-
nsstring alatitude = [(myappdelegate *)[[uiapplication sharedapplication] delegate] latitude]; nsstring alongitude = [(myappdelegate *)[[uiapplication sharedapplication] delegate] longitude]; ps: there many other ways u need, like- having singleton class or saving in ios app file system i.e. in app sandbox..
Comments
Post a Comment