簡體   English   中英

目標c中的單例類

[英]singleton class in objective c

我已經做了一個單例課程,可以通過核心位置獲取位置。 現在我的問題是我想知道位置何時更新。 我想在此使用委托而不是通知。 我知道我可以發布通知。 但我根本不想使用通知。 還有其他方法可以做到這一點,或者對我來說唯一的解決方案是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];

所以在這種情況下,我將如何為單例類設置委托

我不理解您的委托與基於單例模式的類一起使用的問題。

您創建一個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

還有一個將使用所有這些東西的主要方法:

主目錄

#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上調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM