huggie
huggie

Reputation: 18237

Subclass MKMapView and makes it mapview delegate while still allow other delegate

I have a design problem. Here is what I want to do: I want to constraint MKMapView to a specific region, while making it an abstraction for the view controller which want to actually work with the map.

To constraint the map view I most likely want to use the delegate method mapView:regionDidChangeAnimated: and get notified of the changes and move the map back if the region is out of my pre-determined region. However, since I want to make it generic enough I don't want the code to be in view controller. I thought I might want to sub-class MKMapView instead.

If I do that I would have a subclass of MKMapView (say, a ConstraintMapView class) which is also the delegate of MKMapView and expose the methods to constraint the region to any user of the class. But then the user of the class (say a view controller) would also expect to be a delegate of MKMapView, so I would also want to forward all delegate messages to the view controller.

To do so I need a delegate property which points to the real delegate (the view controller), but in my ConstriantMapView if I have one does that mean I'm overriding the MKMapView's setter and getter to the delegate and things get kind of complicated because inside MKMapView it could call ConstraintMapView's methods and I would give it the view controller but I really want to give it ConstraintMapView instead.

  1. Is there a way to make this work?
  2. Is there a better pattern for the problem that spares the controller from the nitty-gritty of moving the view back to the constrainted region?

Upvotes: 1

Views: 1203

Answers (2)

incanus
incanus

Reputation: 5128

I have done a similar proxying MKMapViewDelegate in this project; check it out:

https://github.com/mapbox/mbxmapkit

Upvotes: 1

prakash
prakash

Reputation: 83

If you want to over right an Existing class, you can use "The decorator design pattern". Here is the brief explanation.
http://www.raywenderlich.com/46988/ios-design-patterns
Hope It helps

Upvotes: 0

Related Questions