【问题标题】:singleton class in objective c目标c中的单例类
【发布时间】:2011-08-20 06:26:38
【问题描述】:

我已经制作了一个单例类,用于通过核心位置获取位置。现在我的问题是我想知道位置何时更新。我想在此使用代表而不是通知。我知道我可以发布通知。但我根本不想使用通知。有没有其他方法可以做到这一点,或者对我来说唯一的解决方案是 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];

那么在这种情况下,我将如何为单例类设置委托

【问题讨论】:

  • 查看我的更新答案。这真的很容易。
  • 我确实看到了。但我不明白我会在哪里做这件事。对此感到抱歉:(
  • 最后一次尝试我对单例实例的解释并添加观察者。随意使用 Foundation 将此代码复制粘贴到控制台项目中:)
  • 非常感谢严。我非常感谢这一点。再次感谢

标签: iphone ios4 core-location


【解决方案1】:

我不明白您在使用基于单例模式的类的委托时遇到的问题。

您创建一个 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];

基本示例

这里有一个非常基本的(无内存管理)代码示例,说明单例的工作原理。

协议: 委托协议.h

#import <Foundation/Foundation.h>

@protocol DelegateProtocol <NSObject>

- (void)someMethod;

@end

Singelton 类:

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

你会看到 someMethod-Method 将在 notifyAll 上被调用。

【讨论】:

  • 你的意思是我会这样添加? “[[LocationController locationManager] addObserver: forKeyPath: options: context:]” 我将在哪里听代表或在哪里?
  • 非常感谢您花时间和精力回复我。我对此非常感激
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 2012-02-03
  • 1970-01-01
相关资源
最近更新 更多