[英]Void not working outside viewDidLoad
我正在开始进行iOS编程,并且无法调用void方法。
请不要告诉我MKReverseGeocoding
已过时。 我知道。 我需要使用xcode 3.4和IOS 4,因此,我别无选择。 (32位计算机)。
我做了这个简化的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];
}
如果我从viewDidLoad
调用此void方法,则会找到该地址,并且可以在控制台中以纯文本格式看到它。
- (void)viewDidLoad {
[super viewDidLoad];
............
[self getAdress];
}
但是,如果我尝试从另一个void方法执行此操作,则它将无法正常工作。 我已经尝试了很多事情,但是控制台从未显示地址,尽管我可以在控制台中看到NSLog消息“试图获取地址”。
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
...................
NSLog(@"Trying to get adress");
[self getAdress];
}
我使用的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]);
}
您的问题可能是您的reverseGeocoder var在getAddress方法的末尾超出了范围。 这可能会导致在收到响应之前将对象释放。
您可能考虑做的一件事是使用ivar而不是局部变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.