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 8550319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:55:28+00:00 2026-06-11T13:55:28+00:00

What is the proper way of setting up a separate delegate class for MapKit?

  • 0

What is the proper way of setting up a separate delegate class for MapKit?

I have MapView class subclassing MKMapView and bare MapDelegate class conforming MKMapViewDelegate protocol having only one initializer method.

Here is the extract from MapView initialization method I use:

# MapView.m ...
@implementation MapView

- (id) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {

    // [self setShowsUserLocation:YES];

    [self setDelegate:[[MapDelegate alloc] initWithMapView:self]];

The only method MapDelegate class has is

# MapDelegate.m ...
- (id)initWithMapView:(MapView *)aMapView {
    self = [super init];
    self.mapView = aMapView;
    return self;
}

Having [self setShowsUserLocation:YES]; commented, all works fine – I see the map. If I uncomment this line, my application begins to crash.

What my MapDelegate class is missing?

UPDATE 1: if I don’t use a separate class MapDelegate and set just setDelegate:self – all works.

UPDATE 2: Now I understand, that the problem with [self setDelegate:[[MapDelegate alloc] initWithMapView:self]]; string is that I need MapDelegate class to live longer than it does now (delegate property has weak attribute). If I do the following:

@property (strong) id delegateContainer;
....
[self setDelegateContainer:[[MapDelegate alloc] init]];
[self setDelegate:self.delegateContainer];

…it works! Is there a better way of retaining MapDelegate life cycle along with the one of MKMapView?

Thanks!

  • 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-06-11T13:55:29+00:00Added an answer on June 11, 2026 at 1:55 pm

    After waiting enough for any answers that could appear here and ensuring original problematic behavior twice more times, I am posting my own answer based on the second update from the question:

    The problem with [self setDelegate:[[MapDelegate alloc] initWithMapView:self]]; string is that MapDelegate class should be able to be kept alive outside of the scope of question’s initWithFrame method because delegate property has weak attribute. The possible solution is to create an instance variable serving as a container for a delegate class, for example:

    @property (strong) id delegateClass;
    ....
    [self setDelegateClass:[[MapDelegate alloc] init]];
    [self setDelegate:self.delegateClass];
    

    This solves the original problem.

    LATER UPDATE

    Though it is possible to set MKMapView’s delegate in a separate class, I now realize that such model should not be used:

    Currently I always prefer to use my controllers (i.e. controller layer in MVC in general) as delegates for all of my View layer classes (map view, scroll view, text fields): controller level is the place where all the delegates of different views can meet – all situated in controller layer, they can easily interact with each other and share their logic with the general logic of your controller.

    On the other hand, if you setup your delegate in a separate class, you will need to take additional steps to connect your separate delegate with some controller, so it could interact with a rest part of your logic – this work have always led me to adding additional and messy pieces of code.

    Shortly: do not use separate classes for delegates (at least view classes delegates provided by Apple), use some common places like controllers (fx for views like UIScrollView, MKMapView, UITableView or models like NSURLConnection).

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

Sidebar

Related Questions

What is the proper way to do error-checking in a class? Raising exceptions? Setting
What is the proper way of setting the User-Agent header for a WebClient request
What is the proper way to change Form language at runtime? Setting all controls
What is the proper way of moving a player in Box2D setting Player->ApplyForce() kind
I was looking a proper way to implment a ScaleAnimation. My purpose is to
What's the proper way to add a literal text value from a field to
Is there a proper way, equation or technique in general to say, My web
QUESTION: What is the proper way to use .get() in conjunction with .one() (or
What is the proper way to do the following in clojure? (ns todo.test.models.task (:use
Is there a proper way yet to unit test views with the aspx view

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.