i have made a singleton class for getting locations via core location. now my problem is that I want to know when the location is updated. I want to use delegates rather than Notifications in this. I know I can post notification. but I dont want to use notifications at all. Is there any other way to do this or only solution for me is NSNotifications.
here is some code
//Initilizer
+ (LocationController *)locationManager;
//How I want to be informed using delegates
id<locationControllerDelegate> delegate;
//Instead what I am being forced to use since I dont know how to use delegates with singleton :(
[[NSNotificationCenter defaultCenter] postNotificationName:@"updated" object:nil];
Thank you.
Edit 1:
in typical delegate and simple class we do like this
someClass *somecls = [[someClass alloc] init];
somecls.delegate = self
but in singleton we dont make any instance of class
[[LocationController locationmanager] startUpdateLocation];
So in this case how i will be setting the delegate for the singleton class
I don’t understand your problem using delegates with a singleton pattern based class.
You create a NSMutableArray to store the observers and notify all in a loop if something happened.
Don’t forget to add a removeObserver() Method.
Than you can simply add the delegates via
In your case
Basic Example
So here a very basic (no memory management) code example, of how singleton works.
Protocol:
DelegateProtocol.h
Singelton class:
MySingelton.h
MySingleton.m
And finally the class using the sharedInstance.
SomeClass.h
SomeClass.m
And a main method that will use all this stuff:
main.m
You will see that the someMethod-Method will be called on notifyAll.