首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapViewDelegate派生类和委托赋值

MKMapViewDelegate派生类和委托赋值
EN

Stack Overflow用户
提问于 2011-09-29 01:10:42
回答 2查看 1.2K关注 0票数 2

与iOS相比,这可能更像是一个objective-c问题,但我已经看到了一些类似于下面的示例代码,我想更好地理解它们。

代码语言:javascript
复制
@interface MyMapView : MKMapView <MKMapViewDelegate> {
// ivars specific to derived class
}

@property(nonatomic,assign) id<MKMapViewDelegate> delegate;

@end

@implementation MyMapView
- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // initialize the ivars specific to this class

        // Q1: Why invoke super on this delegate that's also a property of this class?    
        super.delegate = self;

        zoomLevel = self.visibleMapRect.size.width * self.visibleMapRect.size.height;
    }
    return self;
}
#pragma mark - MKMapViewDelegate methods
// Q2: Why intercept these callbacks, only to invoke the delegate?
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    if( [delegate respondsToSelector:@selector(mapView:regionWillChangeAnimated:)] )
    {
        [delegate mapView:mapView regionWillChangeAnimated:animated];
    } 
}

@end

我的两个问题是: 1.为什么要调用super.delegate,并且只将“委托”声明为属性? 2.为什么拦截所有的委托调用,只是将它们转发回委托?

我很感谢你的见解。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-29 03:04:19

苹果公司的文档明确指出,您应该避免使用子类MKMapView

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html#//apple_ref/doc/uid/TP40008205

虽然您不应该子类化MKMapView类本身,但您可以通过提供委托对象来获取有关映射视图行为的信息。

所以我猜这个委托“前进”模式是用来不破坏东西的。

我使用了一种稍微不同的方法来创建MKMapView子类。为了最小化破坏,我使用了两个类。一个是MKMapView的子类,只需覆盖init/dealloc方法,并将delegate属性分配/释放给另一个类的一个实例。另一个类是实现MKMapViewDelegate协议的NSObject的子类,它将执行实际的工作。

MyMapView.h

代码语言:javascript
复制
@interface MyMapView : MKMapView
@end

MyMapView.m

代码语言:javascript
复制
// private map delegate class
@interface MapDelegate : NSObject <MKMapViewDelegate>
// instance is not alive longer then MKMapView so use assign to also solve
// problem with circular retain
@property(nonatomic, assign) MKMapView *mapView;
@end

@implementation MapDelegate
@synthesize mapView;

- (id)initWithMapView:(ReportsMapView *)aMapView {
  self = [super init];
  if (self == nil) {
    return nil;
  }

  self.mapView = aMapView;

  return self;
}

// MKMapViewDelegate methods and other stuff goes here

@end

@implementation MyMapView
- (id)init {
  self = [super init];
  if (self == nil) {
    return nil;
  }

  // delegate is a assign property
  self.delegate = [[MapDelegate alloc] initWithMapView:self];

  return self;
}

- (void)dealloc {
  ((MapDelegate *)self.delegate).mapView = nil;
  [self.delegate release];
  self.delegate = nil;

  [super dealloc];
}
@end

MapDelegate类的mapView属性并不是严格需要的,但如果想要对映射视图做一些事情,而这不是某些MKMapViewDelegate方法调用、计时器等的结果,那么它可能会很有用。

票数 2
EN

Stack Overflow用户

发布于 2011-09-29 02:12:50

  1. 为什么要调用委托,并且只将“super.delegate”声明为属性?Ans。在创建自定义mapview时,调用委托是很重要的,too.We正在调用超类委托来发送来自自定义Mapview的控制。
  2. 为什么拦截所有委托调用只是为了将它们转发回委托?Ans.At我们将控件发送回在超类中声明的委托方法的代码行,以做一些有用的事情。

希望它能解决这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7586961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档