简体   繁体   中英

singleton class in objective c

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.

- (void)addObserver(id<locationControllerDelegate> observer)
{
    [observers addObject: observer];
}

- (void)notifyAll()
{
    for (id<locationControllerDelegate> observer in observers)
    {
        [observer someMethod];
    }
}

Don't forget to add a removeObserver() Method.

Than you can simply add the delegates via

[[MyClass sharedInstance] addObserver:self];

In your case

[[LocationController locationmanager] addObserver:self];

Basic Example

So here a very basic (no memory management) code example, of how singleton works.

Protocol: DelegateProtocol.h

#import <Foundation/Foundation.h>

@protocol DelegateProtocol <NSObject>

- (void)someMethod;

@end

Singelton class:

MySingelton.h

#import <Foundation/Foundation.h>
@protocol DelegateProtocol;

@interface MySingleton : NSObject{
    NSMutableArray *observers;
}

+ (MySingleton *)sharedInstance;
- (void)addObserver:(id<DelegateProtocol>) observer;
- (void)notifyAll;
@end

MySingleton.m

#import "MySingleton.h"
#import "DelegateProtocol.h"

@implementation MySingleton

static MySingleton *sharedInstance;

- (id)init
{
    self = [super init];
    if (self) {
        observers = [[NSMutableArray alloc] init];
    }

    return self;
}

+ (MySingleton *)sharedInstance
{
    if (sharedInstance == NULL) {
        sharedInstance = [[MySingleton alloc] init];
    }

    return sharedInstance;
}

- (void)addObserver:(id<DelegateProtocol>)observer
{
    [observers addObject:observer];
}

- (void)notifyAll
{
    for(id<DelegateProtocol> observer in observers) {
        [observer someMethod];
    }
}

@end

And finally the class using the sharedInstance.

SomeClass.h

#import <Foundation/Foundation.h>
#import "DelegateProtocol.h"

@interface SomeClass : NSObject <DelegateProtocol>

@end

SomeClass.m

#import "SomeClass.h"
#import "DelegateProtocol.h"
#import "MySingleton.h"

@implementation SomeClass

- (id)init
{
    self = [super init];
    if (self) {
    }

    return self;
}

- (void)someMethod
{
    NSLog(@"Called from singleton!");
}

@end

And a main method that will use all this stuff:

main.m

#import <Foundation/Foundation.h>
#import "SomeClass.h"
#import "MySingleton.h"

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    SomeClass *aClass = [[SomeClass alloc]init];
    [[MySingleton sharedInstance] addObserver:aClass];
    [[MySingleton sharedInstance] notifyAll];

    [pool drain];
    return 0;
}

You will see that the someMethod-Method will be called on notifyAll.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM