Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:39:09+00:00 2026-05-25T20:39:09+00:00

This is probably more of an objective-c question over iOS but I’ve seen some

  • 0

This is probably more of an objective-c question over iOS but I’ve seen some example code similar to the following that I’d like to better understand.

@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

My two questions are:
1. Why would one invoke the super.delegate and also only declare the ‘delegate’ as a property?
2. Why intercept all of the delegate calls only to forward them back to the delegate?

I appreciate any insights.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:39:09+00:00Added an answer on May 25, 2026 at 8:39 pm

    Apple’s documentation explicitly states that you should avoid subclass MKMapView:

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

    Although you should not subclass the MKMapView class itself, you can
    get information about the map view’s behavior by providing a delegate
    object.

    So i guess this delegate “forward” pattern is used to not break things.

    I use a little different approach to subclass MKMapView. To minimize breakage i use two classes. One that subclass MKMapView and just override the init/dealloc method and assign/release the delegate property to a instance of the other class. The other class is a subclass of NSObject that implements the MKMapViewDelegate protocol and will be the one that does the real work.

    MyMapView.h

    @interface MyMapView : MKMapView
    @end
    

    MyMapView.m

    // 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
    

    The mapView property for MapDelegate class is not strictly needed but is probably useful if want to do things to the map view that that is not a result of some MKMapViewDelegate method call, timers etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a common Objective-C question reported by Java coders, but I don't
This is probably more a design or usage question but the main issue is
This is probably more of a JavaScript question but I'd like to pass another
This is probably more of a usability question but this seems like a good
This is probably more of a best practices question than a true code question.
This is probably more a Gradle question than a Caliper question, but I am
Okay this is probably more a maths question but since its related to programming
Probably I cant get more dumb, but seriously this is the first time I
This probably is a dummy question but I cannot find a clear indication. I
Im sorry for this probably dumm question, but I want to simply open modals

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.