I have set my appDelegate class as the delegate for CLLocationManager. In the didUpdateLocations method of the delegate, I create a polyline. I add the polyline to the map view using [self.firstViewController.currentMap addOverlay:self.polyline level:MKOverlayLevelAboveRoads]. However, it has no effect on the actual instance of my map view because it thinks that self.firstViewController.currentMap is nil. Is there a way to somehow pass the instance of the map to didUpdateLocations so that it adds the overlay to the existing instance of the MKMapView?
You can't pass extra parameters in delegate methods. The method signatures of the delegate methods are fixed.
However, that's not the correct solution. It sounds like you got help in the comments that's enabled you to fix this though.
Related
In my UIViewController I use both MKMapView and a MGLMapView. I need to use both of their delegate methods including mapView:didAddAnnotationViews: which is named the same for both maps. Xcode doesn't like this:
Is it possible to use both of these delegate methods in the same viewController?
I'm not sure if this will work, but you could try declaring the method just once, like this:
- (void)mapView:(id)mapView didAddAnnotationViews:(id)views {}
And inside the method you check the class of the mapView and do what you must do in each case
I am working on a sport project at the moment.
What i want to do is, when users select the AutoPause switch on, the CLLocationManager will pause updating location when speed is below a certain level.
Basically, i have figure out how to implement the locationManager by changes its attribute, but my question is, how can I set CLLocationManager's attribute from the settingViewController, whereas the CLLocationManager instance is in another ViewController. Thanks in advance.
You can use NSNotificationCenter to send notification to enable/ disable the CLLocationManager's autopause attribute in another View Controller.
Other approaches can be:
Use class method, it is explained very well in this SO Answer
Use Delegates
idk what' your problem with CLLocationManager, do you mean the way to pass object to another view controller? there are several way to do this.See this question:Passing Data between View Controllers
I'm pretty sure that you can pass the CLLocationManager object to settingViewController by setting a property of that CLLocationManager,because passing the object means pass the reference the object,you can change the object in settingViewController life cycle,and it affects the CLLocationManager object which created by ViewController.
I have a master-detail application, and my details page is a map. When I click on the element in the Master list, the map with whole bunch of markers shows up, zooms/pans into a specific location, and an annotation pops up describing what this location is.
The implementation is pretty simple, so I thought. In my viewDidAppear, I go through the list of annotations in my mapview and just call [mapView selectAnnotation: myAnnotation animated: FALSE] and it works fine. BUT NOT THE FIRST TIME!
I populate my map in ViewDidLoad, and the first time the ViewDidAppear is called, the mapView.annotations array is still empty. Is there a simple way to make it work even when I first enter the details view? Or do I have to create and populate the map in the AppDelegate?
Try using MGLMapViewDelegate and especially
func mapViewDidFinishLoadingMap(mapView: MGLMapView) {
// your code here
}
Maybe you can define a method at the DetailViewController,then when you trigger the method like tableview's didselect or prepareForSegue,you must get the DetailViewController's instance,finally,you can use this instance to call your user-defined method.
Sorry this question may sound "subjective" but I think it should have a pretty definitive answer. I have a class "LocationManager" that I want to manage my Core Location logic. I have two options:
LocationManager has a strong property referencing an instance of CLLocationManager. LocationManager is a delegate of CLLocationManager and receives location updates from it as such.
LocationManager is a subclass of CLLocationManager, and says self.delegate = self so that it can receive its own location updates.
I'm curious which of these options is considered the "right" thing to do, I'm sure that there must a be a preferred way. Thanks!
Subclassing CLLocationManager and setting its delegate to self should not be done because it breaks the contract of CLLocationManager. As the class is currently defined, it has a delegate property. This property serves as a contract which states that you may set this property to some other object, and this object will receive delegate notifications. If you subclass CLLocationManager (let's call it MyLocationManager), and if the delegate property of the object points to itself, then you will most likely create a situation where MyLocationManager only works as promised if the user does not use the delegate property for his own purposes. From a users point of view, MyLocationManager is a CLLocationManager without a usable delegate property. This violates Liskovs Substitution Principle, btw. The question to ask here is: would MyLocationManager still work, if some ViewController class decides to use it and have its delegate property point to itself (the ViewController)?
Furthermore, it is no longer "delegation", if you say self.delegate = self. So I would say it is preferrable to use variant 1.
Thanks for the question.
Yes you can do this with no problem. I've a subclass of UITextField which is its own delegate.
The first option seems right to me because it doesn't make a ton of sense to subclass CLLocationManager (#2). What functionality would you be adding to it? If you're not adding anything to it why subclass?
All you care about is encapsulating the messages about location updates. I'd say you're using the delegate/protocol pattern acceptably in the first case.
And Jef is right, there are times where a subclass of another class can be set as its own delegate. Though you need to be careful about how that object responds to certain messages.
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.
Is there a way to make this work?
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?
I have done a similar proxying MKMapViewDelegate in this project; check it out:
https://github.com/mapbox/mbxmapkit
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