2011-04-08 77 views
6

嘿家伙,我想问一下,实际上sharedInstance是什么?我的意思是什么用法?关于SharedInstance的问题

目前我在通信2个不同的文件时遇到了一些问题。

我的问题是: 我有1个文件调用AH/AM与另一个文件调用了Bh/BM 啊需要访问一些在波黑的数据,所以....是否有任何可能的方式我能达到什么样的我想要? 只是想知道“SharedInstance”能够解决我的问题吗?

寻找回复:)

回答

11

sharedInstance可用于多种方式。

例如,您可以从静态上下文访问对象。实际上,它被用来支持Singleton模式。 这意味着在整个程序代码中只使用了该类的一个对象,只有一个实例。

接口可以关注一下:

@interface ARViewController 
{ 
} 
@property (nonatomic, retain) NSString *ARName; 

+ (ARViewController *) sharedInstance; 

实施ARViewController:

@implementation ARViewController 
static id _instance 
@synthesize ARName; 
... 
- (id) init 
{ 
    if (_instance == nil) 
    { 
     _instance = [[super allocWithZone:nil] init]; 
    } 
    return _instance; 
} 

+ (ARViewController *) sharedInstance 
{ 
    if (!_instance) 
    { 
     return [[ARViewController alloc] init]; 
    } 
    return _instance; 
} 

并访问它,使用类CustomARFunction如下:

#import "ARViewController.h" 

@implementation CustomARFunction.m 

- (void) yourMethod 
{ 
    [ARViewController sharedInstance].ARName = @"New Name"; 
} 
+0

我不明白:( – Birdkingz 2011-04-08 08:27:24

+0

好吧,请向我们提供一些更详细的信息,问题出在哪里,什么你到底不懂:) – Lepidopteron 2011-04-08 08:32:19

+0

OK,我有2个文件 - ARViewController。 h /.m - CustomARFunction .h /.m 在ARViewController.h里面,我有一个字符串变量调用ARName。 我想要的是实际上在CustomARFunction.m中,我需要在ARViewController.h中引用ARName并对其进行一些更改。意思是,如果我使用CustomARFunction.m更改ARName,则ARViewController中的ARName也会更改。 – Birdkingz 2011-04-08 08:41:46

3

一个sharedInstance往往实现与singleton pattern。像在[UIApplication sharedApplication] - >只有一个应用程序,你通过这个单身人士访问。

这个想法是让一个类的实例可以通过调用一个类方法来访问,在objective-c中通常命名为sharedXXX。

为了解决你的问题,你实际上可以实现一个模型类的单例,并且可以用一个静态方法访问一个已存在的实例来写入和访问数据,我们称之为sharedModel。

改进模型和更新视图的下一步是KVO:关键值观察。你在你的viewController中添加一个观察者来“观察”对你的模型所作的改变。如果发生这种变化,你的viewController会被通知,然后你可以更新视图。

您可以通过Apple's documentationmindsizzlers了解有关KVO的更多信息,这里有一个小巧简单的教程。

1

接口

@interface CouponSynchronizer : NSObject 

+ (CouponSynchronizer *) sharedSynchronizer; 

@end 

实施

@implementation CouponSynchronizer 

static CouponSynchronizer *sharedSynchronizer = nil; 

+ (CouponSynchronizer *)sharedSynchronizer 
{ 
    @synchronized(self) { 
     if (sharedSynchronizer == nil) { 
      sharedSynchronizer = [[self alloc] init]; 
     } 
    } 
    return sharedSynchronizer; 
} 

@end 
7

共享实例是,通过它可以访问相同的实例或对象的类的任何地方在项目的过程。这背后的主要思想是每次调用某个方法时返回相同的对象,以便存储在实例中的值/属性可以在应用程序中的任何位置使用。

这可以在2个简单的过程中完成如下: -

1)使用一个静态变量初始化一次

@implementation SharedInstanceClass 

static SharedInstanceClass *sharedInstance = nil; 

+ (id)sharedInstanceMethod 
{ 
    @synchronized(self) { 
     if (sharedInstance == nil) { 
      sharedInstance = [SharedInstanceClass new]; 
     } 
    } 
    return sharedInstance; 
} 

@end 

2)使用GCD的: -

+ (id)sharedInstance{ 
    static dispatch_once_t onceToken; 
    static SharedInstanceClass *sharedInstance = nil; 
    dispatch_once(&onceToken, ^{ 
     sharedInstance = [SharedInstanceClass new]; 
    }); 
    return sharedInstance; 
} 

这些必须被称为: -

SharedInstanceClass *instance = [SharedInstanceClass sharedInstance]; 

因此,每次从函数返回相同的实例,并且设置为属性的值将被保留,并且可以在应用程序中的任何位置使用。

问候,