2012-03-01 4 views
-1

방금 ​​Objective C로 코딩을 시작했고, 사용자 위치를 추적 할 앱을 만들고, 그 안에 latlng으로 경고를 보냅니다. 이 코드는 조금이라도 완벽하지는 않지만 경고를 위해 "viewDidLoad"에서 "lat"를 만든 변수를 사용하는 데 문제가 발생했습니다. CLLocationManager 대리자/메서드에서 변수를 선언했는데 다른 곳에서 사용하는 방법을 모르겠습니다.다른 메소드에서 선언 된 변수를 다른 메소드로 사용하기

- (void)viewDidLoad 
{ 
    locationManager =[[CLLocationManager alloc] init]; 

locationManager.delegate = self; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
locationManager.distanceFilter = 100.0f; 
[locationManager startUpdatingLocation]; 


UIAlertView *message = [[UIAlertView alloc] initWithTitle: @"Your current Latitude is:" 
                message:This is where I want to put the variable "lat" 
               delegate:nil 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
[message show];  

[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

은}

-(void) locationmanager: (CLLocationManager *) manager 
     didUpdateToLocation: (CLLocation *) newLocation 
     fromLocation: (CLLocation *) oldLocation 
{ 
    NSString *lat = [[NSString alloc] initWithFormat: @"%g",newLocation.coordinate.latitude]; 
    NSString *lng = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude]; 
} 

어떤 도움을 주시면 감사하겠습니다!

+0

이 모든 클래스가 있습니까? –

+0

예, 그렇게 생각합니다. – michael03m

+0

.h 파일에서 선언 한 변수를 @synthesize하는 것을 잊어 버렸습니다. – michael03m

답변

0

간단한 해결책. 이

@interface ViewController : UIViewController { 
    NSString *lat; 
    NSString *lng; 
} 

처럼 보일 것이다 .H 귀하의 ViewController는 다음이 당신이 코드

을 제거하는 곳 바로 아래 위치입니다 콘솔에

-(void) locationmanager: (CLLocationManager *) manager 
    didUpdateToLocation: (CLLocation *) newLocation 
    fromLocation: (CLLocation *) oldLocation 
{ 
    lat = [[NSString alloc] initWithFormat: @"%g",newLocation.coordinate.latitude]; 
    lng = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude]; 
    NSLog(lat); 
    NSLog(lng); 
} 

NSlog 밖으로 간단한 풋이된다

UIAlertView *message = [[UIAlertView alloc] initWithTitle: @"Your current Latitude is:" 
               message:This is where I want to put the variable "lat" 
              delegate:nil 
            cancelButtonTitle:@"OK" 
            otherButtonTitles:nil]; 
[message show];  

이렇게하면 알림이 s 보기가로드되면 oon이로드됩니다.

+0

그래서 NSLog는 newLocation을 가질 때마다 콘솔로 출력할까요? – michael03m

+0

@Michael : NSLog 호출이 올바르지 않습니다. NSLog에 대한 첫 번째 인수는 형식 문자열이어야합니다. –

+0

사실 NSLog 호출은 문제가 없습니다. 나는 거기에서 그들을 가장 엄격한 의미로 사용하지 않고 있었다. 100 % 정확하려면 NSLog (@ "% @", lat)를 사용하십시오. NSLog (@ "% @", lng); –

관련 문제