【问题标题】:Void not working outside viewDidLoad无效在 viewDidLoad 之外工作
【发布时间】:2015-01-06 01:03:36
【问题描述】:

我正在开始 iOS 编程,但我一直在调用 无效方法。

请不要告诉我 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]);
}

【问题讨论】:

  • 问题绝对不是你想的那样……你可以调用一个从任何方法返回“void”的方法。您在哪里/如何尝试记录结果?
  • 请花时间阅读一些基本的编程书籍,例如“C”编程语言。哦,void 只是说该方法没有返回任何内容,您没有调用,也没有void
  • @Zaph 但平心而论,尽管 OP 显然对某些事情感到困惑,但您提出的问题显然只是语义问题。如果您通过 OP 的问题并将“void”替换为“void method”,那么您的反对意见将不成立。事实上,我现在就进行编辑。
  • 感谢您的指正。我只是使用控制台来查看发生了什么。如果我在 viewDidLoad 括号内使用 [self getAdress],控制台将显示地址。如果我在另一个 void 方法中使用它,则不会发生这种情况。
  • 您能否分享一下您对反向地理编码器委托方法的实现(-reverseGeocoder:didFindPlacemark:-reverseGeocoder:didFailWithError:)?此外,使用断点可以帮助您更好地了解流程并可能找到问题的根源。

标签: ios objective-c void viewdidload


【解决方案1】:

您的问题可能是您的 reverseGeocoder var 在 getAddress 方法结束时超出范围。这可能会导致对象在您收到响应之前被释放。

您可能会考虑使用 ivar 而不是局部变量。

【讨论】:

  • 尝试将 MKReverseGeocoder *reversegeocoder 声明为 .h 文件中的 ivar,但此时没有更改。也许这是我的声明...我需要阅读更多文档。我觉得你的答案很好,但我现在无法确认。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多