2011-04-18 2 views
0

이것은 내 첫 번째 게시물이지만 항상 사이트를 읽습니다. 여러 클래스 사이에 CLLocation *을 전달하는 프로그램을 다루고 있습니다. CLLocationManager를 사용하여 현재 위치를 찾는 앱 위임자로 시작합니다. 위치가 발견되면 didUpdateToLocation :를 newLocation : 나는 위치를 기록 할 수 있습니다, 여기에CLLocation iVar 값을 잃어 버리고, 프로그램 중간에 null이 됨

//AppDelegate.m 
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *)oldlocation { 
    currentLocation = newLocation; 
    [queryPage setLocation:currentLocation]; 
} 

, 모든 것이 잘 작동 : fromLocation 방법은 다른 클래스라는 queryPage이 위치를 설정합니다. 그런 다음 queryPage에서 setLocation은 CLLocation을 받고 문제없이 로그 할 수 있습니다. 그러나 나중에 내 코드에서 button1 및 button2와 같은 메서드는 currentLocation에 액세스하는 데 문제가 있습니다. button1은 때때로 그것을 기록 할 수 있고, button2는 보통 "(null)"을 기록합니다.

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

@interface QueryPage : UIViewController { 
    CLLocation *currentLocation; 
} 
@property (nonatomic, retain) CLLocation *currentLocation; 
-(void)setLocation:(CLLocation*)location; 

과 implenentation 파일 : 여기에 QueryPage.h입니다

//QueryPage.m 
#import QueryPage.h 
@implementation QueryPage 
@synthesize currentLocation; 

... 

-(void)setLocation:(CLLocation*)location 
{ 
    self.currentLocation = location; 
} 

... 

-(IBAction)button1:(id)sender 
{ 
    NSLog(@"button1: %@", currentLocation); 
} 

... 

-(IBAction)button2:(id)sender 
{ 
    NSLog(@"button2: %@", currentLocation); 
} 

내가 currentLocation 길을 잘못 유지/설정해야한다고 생각하지만, 내가 어떻게 아무 생각이 없습니다. 나는 기억을 적절하게 다루려고 노력하는 날을 수색했으며 이것은 내가 얻은 것까지이다. 나는 currentLocation과 내가 온라인에서 찾을 수있는 많은 다른 것들을 유지하려고 노력했다. 나는 IB의 연결이 좋다는 것을 안다. (나는 그것들을 만질 때마다 그것을 기록한다.) 그리고 어떤 버튼이 건드리기 전에 currentLocation은 항상 설정된다. (나는 그것을 로그에서 볼 수있다.) 아무에게도 이걸 도와 줄 수 있으면 알려주세요. 이것은 포럼의 다른 문제와 유사하지만 지금까지 권장 된 모든 것을 시도했습니다. 감사!

답변

0

비 원자력을 제거해야합니다.

+0

또한 setLocation 메소드가 필요하지 않습니다. synthesize에 의해 생성 된 setCurrentLocation을 사용하면됩니다. – picciano

+0

죄송합니다, 한가지 더. currentLocation이 아닌 NSLog 문에서 self.currentLocation을 사용해야합니다. – picciano

+0

감사합니다 picciano! 잘 했어. 빠른 답변 주셔서 감사합니다. – alexshafran

관련 문제