這可能更多的是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.爲什麼只攔截所有的委託來電轉回委託?
我很欣賞任何見解。