簡體   English   中英

無效在viewDidLoad外部不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM