目标c中的单身类

时间:2011-08-20 06:26:38

标签: iphone ios4 core-location

我已经通过核心位置创建了一个单例类来获取位置。现在我的问题是我想知道位置何时更新。我想在此使用委托而不是通知。我知道我可以发布通知。但我根本不想使用通知。有没有其他方法可以做到这一点,或者只有我的解决方案是NSNotifications。

这是一些代码

//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];

谢谢。

编辑1:

在典型的委托和简单的课程中我们都喜欢这个

someClass *somecls = [[someClass alloc] init];
somecls.delegate = self

但是在单身中我们不会创建任何类

的实例
[[LocationController locationmanager] startUpdateLocation];

所以在这种情况下我将如何为单例类设置委托

1 个答案:

答案 0 :(得分:8)

我不理解使用基于单例模式的代理的代理问题。

您创建一个NSMutableArray来存储观察者,并在发生事件时通知所有人。

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

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

不要忘记添加removeObserver()方法。

您可以通过

简单地添加代表
[[MyClass sharedInstance] addObserver:self];

在你的情况下

[[LocationController locationmanager] addObserver:self];

基本示例

所以这里是一个非常基本的(无内存管理)代码示例,单例如何工作。

协议: DelegateProtocol.h

#import <Foundation/Foundation.h>

@protocol DelegateProtocol <NSObject>

- (void)someMethod;

@end

辛格尔顿班:

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

最后使用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

这是一个使用所有这些东西的主要方法:

的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;
}

您将看到将在notifyAll上调用someMethod-Method。