Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 目标c中的单例类_Iphone_Ios4_Core Location - Fatal编程技术网

Iphone 目标c中的单例类

Iphone 目标c中的单例类,iphone,ios4,core-location,Iphone,Ios4,Core Location,我制作了一个通过核心位置获取位置的单例类。现在我的问题是,我想知道位置何时更新。我想在这里使用委托而不是通知。我知道我可以发布通知。但是我根本不想使用通知。有没有其他方法可以做到这一点,或者对我来说唯一的解决方案就是通知 这里有一些代码 //Initilizer + (LocationController *)locationManager; //How I want to be informed using delegates id<locationControllerDelegat

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

这里有一些代码

//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];
但是在singleton中,我们不做任何类的实例

[[LocationController locationmanager] startUpdateLocation];

因此,在本例中,我将如何为singleton类设置委托

我不理解您在使用基于singleton模式的类的委托时遇到的问题

您可以创建一个NSMutableArray来存储观察者,并在发生事件时通知循环中的所有人

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

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

[[LocationController locationmanager] addObserver:self];
基本示例 所以这里有一个非常基本(无内存管理)的代码示例,说明了单例如何工作

协议: 授权协议

#import <Foundation/Foundation.h>

@protocol DelegateProtocol <NSObject>

- (void)someMethod;

@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;
}
#导入
#导入“SomeClass.h”
#导入“MySingleton.h”
int main(int argc,const char*argv[]
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
SomeClass*aClass=[[SomeClass alloc]init];
[[MySingleton sharedInstance]添加观察者:aClass];
[[MySingleton sharedInstance]notifyAll];
[泳池排水沟];
返回0;
}

您将看到someMethod方法将在notifyAll上被调用。

我不理解使用基于单例模式的类的委托时出现的问题

您可以创建一个NSMutableArray来存储观察者,并在发生事件时通知循环中的所有人

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

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

[[LocationController locationmanager] addObserver:self];
基本示例 所以这里有一个非常基本(无内存管理)的代码示例,说明了单例如何工作

协议: 授权协议

#import <Foundation/Foundation.h>

@protocol DelegateProtocol <NSObject>

- (void)someMethod;

@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;
}
#导入
#导入“SomeClass.h”
#导入“MySingleton.h”
int main(int argc,const char*argv[]
{
NSAutoreleasePool*池=[[NSAutoreleasePool alloc]init];
SomeClass*aClass=[[SomeClass alloc]init];
[[MySingleton sharedInstance]添加观察者:aClass];
[[MySingleton sharedInstance]notifyAll];
[泳池排水沟];
返回0;
}

您将看到在notifyAll上调用someMethod方法。

查看我的更新答案。这真的很容易。是的,我看到了。但我不明白我将在哪里这样做。很抱歉:(最后一次尝试我的解释是在一个单独的实例上添加观察者。可以自由地将这个代码复制到一个使用基础的控制台项目中):非常感谢严。我非常感激。再次感谢您查看我的最新答案。这真的很容易。是的,我看到了。但我不明白我将在哪里这样做。很抱歉:(最后一次尝试我的解释是在一个单独的实例上添加观察者。可以自由地将这个代码复制到一个使用基础的控制台项目中):非常感谢严。我非常感激。再次谢谢你的意思是我想加上这个?“[[LocationController locationManager]addObserver:forKeyPath:options:context:”我将在何处聆听代表发言?非常感谢您花费时间和精力回复我。我非常感激你的意思是说我想补充一下?“[[LocationController locationManager]addObserver:forKeyPath:options:context:”我将在何处聆听代表发言?非常感谢您花费时间和精力回复我。对此我非常感激
#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;
}