简体   繁体   中英

Void not working outside viewDidLoad

I'm beginning iOS programming and I'm stuck with calling a void method.

Please don't tell me that MKReverseGeocoding is deprecated. I know that. I need to use xcode 3.4 and IOS 4, so, I have no choices. (32 bits computer).

I have made this simplified void method:

-(void)getAdress
{
    CLLocationDegrees lat=37.4217080;
    CLLocationDegrees lon=-122.0829964;            
    CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(lat, lon);        
    MKReverseGeocoder *reverseGeocoder=[[MKReverseGeocoder alloc] initWithCoordinate:coord];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];
}

If I call this void method from viewDidLoad , the address is found and I can see it in the console in plain text.

- (void)viewDidLoad {
    [super viewDidLoad];
        ............
    [self getAdress];
}

But if I try to do it from another void method it doesn't work. I have tried many things but the console never shows the address, although I can see the NSLog message "Trying to get adress" in the console...

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    ...................
    NSLog(@"Trying to get adress");
    [self getAdress];
}

The reverseGeocoder methods i use :

- (void)reverseGeocoder:(MKReverseGeocoder *)reverseGeocoder didFailWithError:(NSError *)error {
    NSLog(@"The geocoder has returned: ERROR%@");
}

- (void)reverseGeocoder:(MKReverseGeocoder *)reverseGeocoder didFindPlacemark:(MKPlacemark *)placemark {
    NSLog(@"Reverse Adresse : %@", [placemark addressDictionary]);
}

Your problem might be your reverseGeocoder var goes out of scope at the end of getAddress method. This might cause for the object to be deallocated before you receive the response.

One thing you may consider doing is using an ivar instead of local variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM