Использование переменной, объявленной где-то еще в другом методе - PullRequest
0 голосов
/ 01 марта 2012

Я только начал писать код в Objective C и создаю приложение, которое будет отслеживать местоположение пользователей и отправлять оповещения с последними данными.Этот код ни в коем случае не завершен, но я столкнулся с проблемой при попытке использовать переменную, которую я создал "lat" в "viewDidLoad" для предупреждения.Я объявил переменную в делегате / методе 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];  
} 

Буду признателен за любую помощь!

1 Ответ

0 голосов
/ 01 марта 2012

Простое решение. Ваш ViewController .h должен выглядеть примерно так

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

тогда это становится

-(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 simple out выводит на консоль, которая находится чуть ниже, где вы кодируете

удалить

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];    

Потому что при загрузке представления будет отображаться предупреждение

Надеюсь, это поможет

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...