How to set CLLocationManager's attribute from other class - ios

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.

Related

iOS Delegates instead of passing data through a segue

I'm trying to learn how delegates work and wrap my head around the concept. I'm finding I get some of the ideas. I understand you use it to pass data from one view controller to another, however wouldn't it work the same if I just sent data from a segue and every time the 1st view controller would appear, it would use that data?
So for example I have 2 view controllers.
1 is homeViewController and
2 is editViewController.
I have a variable titled "addressOfHome" which is a string in homeViewController(1).
In homeViewController under the method "viewDidAppear"
I also set the addressLabel = addressOfHome.
Then I just pass the data from editViewController(2) to homeViewController(1)
when the segue's destination vc is homeViewController ?
I'm terrible at explaining things so I apologize for that, but I gave it my best shot. Thanks for your time!
Delegates are mainly used to "trigger action" on an object from another one.
An object delegates a way to handle something to someone else, for example when you click on an UIAlertView button, if its delegate is set on a viewController, alertView:clickedButtonAtIndex will be executed on the VC, which can so react as it want
I'm terrible at explaining things
Haha, yes, you are !
A delegate isn't for that - a delegate is a way to over-ride the default behaviour of some feature(s) of a class, without creating your own sub-class. See this post : How does a delegate work in objective-C?
Are you trying to understand how delegates work (in which case, I don't think your example is one that requires a delegate) or are you trying to implement the functionality you describe, and think that a delegate is the way to do it? (I think you actually want a data source).

Does a class being its own delegate follow iOS convention?

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.

How to pass instance of MKMapView to CLLocationManagerDelegate?

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.

How to notify UITableView on data download complete from AppDelegate when NSNotification is not an option

I have a method in AppDelegate which get some data from server, this method get called every time when application become active. I want to reload some table in another view when server data received successfully. How can i do this without using NSNotification?. I know passing notification can do this job. I want to know is there any other way to perform this?
well comments by users already explained what to do.
But i had another approach.I don't prefer, but it will work.
Always set some constant tag value to your table view (say 1001).And make sure than you never use the same tag on others.
Then in that method of appDelegate, you can do->
UITableView *tableView =(UITableView*)[self.window viewWithTag:1001];
[tableView reloadData];
I am reminding you again, don't use this. use NSNotification class.
Although it creates an unneeded dependency between the view and the app delegate, I'd implement this way:
create a ViewReloader protocol with a method - (void) reloadTable;
implement that protocol in the view
add a property of type id<ViewReloader> to the app delegate
when the view is instantiated, assign it to the property defined above
when you need to reload, call the reloadTable method of the id<ViewReloader> property from the app delegate (but always check for property != nil)
if the view is destroyed/deallocated, remember to reset the app delegate property
Well samething you can do with using custom delegates as well. If you want to pass message from one object to another. You can use notification also, but use only when you want to broadcast the message.

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

Resources