简体   繁体   English

无效在viewDidLoad外部不起作用

[英]Void not working outside viewDidLoad

I'm beginning iOS programming and I'm stuck with calling a void method. 我正在开始进行iOS编程,并且无法调用void方法。

Please don't tell me that MKReverseGeocoding is deprecated. 请不要告诉我MKReverseGeocoding已过时。 I know that. 我知道。 I need to use xcode 3.4 and IOS 4, so, I have no choices. 我需要使用xcode 3.4和IOS 4,因此,我别无选择。 (32 bits computer). (32位计算机)。

I have made this simplified void method: 我做了这个简化的void方法:

-(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. 如果我从viewDidLoad调用此void方法,则会找到该地址,并且可以在控制台中以纯文本格式看到它。

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

But if I try to do it from another void method it doesn't work. 但是,如果我尝试从另一个void方法执行此操作,则它将无法正常工作。 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... 我已经尝试了很多事情,但是控制台从未显示地址,尽管我可以在控制台中看到NSLog消息“试图获取地址”。

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

The reverseGeocoder methods i use : 我使用的reverseGeocoder方法是:

- (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. 您的问题可能是您的reverseGeocoder var在getAddress方法的末尾超出了范围。 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. 您可能考虑做的一件事是使用ivar而不是局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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