这可能更多的是iOS上的objective-c问题,但我已经看到了一些类似于下面的代码,我想更好地理解它。MKMapViewDelegate派生类和委托分配
@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.为什么只拦截所有的委托来电转回委托?
我很欣赏任何见解。