简体   繁体   English

MKA注释协议

[英]MKAnnotation protocol

Im currrently going through a geolocation tutorial which adopts the MKAnnotation Protocol into a class. 我目前正在遍历一个地理定位教程,该教程将MKAnnotation协议引入了一个类。

The tutorial suggests to create the following methods in the Theannotation.h class 本教程建议在Theannotation.h类中创建以下方法

+ (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord; 
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord;

and in the implementation 并在实施中

  + (id)annotationWithCoordinate:(CLLocationCoordinate2D)coord {

return [[[[self class] alloc] initWithCoordinate:coord] autorelease];
  }


- (id)initWithCoordinate:(CLLocationCoordinate2D)coord {
    if ( self = [super init] ) {
    self.coordinate = coord;
}
return self;

} }

The second method is then called in a viewcontroller 然后在视图控制器中调用第二种方法

  Theannotation *annotation = [[SimpleAnnotation alloc] initWithCoordinate:Coords];

I understand the second method completely however Im puzzled to the inclusion of the first. 我完全理解第二种方法,但是我对加入第一种方法感到困惑。 The class method isn't called at any other place in the example tutorial and im struggling to understand why you would use a class method in this case. 在示例教程中的任何其他地方都没有调用class方法,并且很难理解在这种情况下为什么要使用class方法。

You can omit this class method but in some cases it is useful because it provides you a mechanism to create 'temporary' annotation that will be autoreleased. 您可以省略该类方法,但在某些情况下它很有用,因为它为您提供了一种创建将自动发布的“临时”注释的机制。 Of course you can do it manually, but class method is a way of convenience in that case. 当然,您可以手动执行此操作,但是在这种情况下,类方法是一种便捷的方法。

please go through this blog here 在这里浏览这个博客

or you can download the code- link 或者您可以下载代码链接

and see the code, you will know that which things are mandatory and which will not. 并查看代码,您将知道哪些是强制性的,哪些不是强制性的。

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

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