使用单例创建可以由多个视图访问的数组 [英] Using a singleton to create an array accessible by multiple views

查看:102
本文介绍了使用单例创建可以由多个视图访问的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个经典的问题。



我想从应用程式内任何地方存取物件阵列。我也想这样做使用单身。我的问题是:


  1. 我在哪里实例化我的单例对象?

  2. NSMutable对象数组?

  3. 如何在项目中的任何地方引用此数组?

所有代码和示例都非常感谢!



编辑1



是我到目前为止。我不知道如何正确和一致地访问这个香蕉阵列:

  #import基础/基础。 h。 

@interface Singleton:NSObject {
NSMutableArray * bananas;
}

@property(nonatomic,retain)NSMutableArray * bananas;

@end


#importSingleton.h

static Singleton * mySingleton;

@implementation单例

@synthesize bananas;

#pragma mark SingletonDescption stuff

+(Singleton *)mySingleton
{
if(!mySingleton){
mySingleton = [[ Singleton alloc] init];
}

return mySingleton;
}

+(id)allocWithZone:(NSZone *)zone
{
if(!mySingleton){
mySingleton = [super allocWithZone:zone ];
return mySingleton;
} else {
return nil;
}
}

- (id)copyWithZone:(NSZone *)zone
{
return self;
}

- (void)release
{
// NO OP
}

@end

EDIT 2



是我如何使用我的单例对象有一个对象数组放置在表单元格。没有发生任何事情,表格单元格空白:(

   - (id)init 
{
[super initWithStyle:UITableViewStylePlain];

// bananas = [[NSMutableArray alloc] init];

Singleton * mySingleton = [[Singleton alloc] init];
mySingleton.bananas = [[NSMutableArray alloc] init];

UIImage * imageA = [UIImage imageNamed:@A.png];
UIImage * imageB = [UIImage imageNamed: bp];
UIImage * imageC = [UIImage imageNamed:@C.png];

Banana * yellowBanana = [[Banana alloc] initWithName:@描述:@Beautifulweight:22.0 icon:imageA];
Banana * greenBanana = [[Banana alloc] initWithName:@Greendescription:@Gorgeousweight:12.0 icon:imageB];
Banana * rottenBanana = [[Banana alloc] initWithName:@Rottendescription:@Uglyweight:8.0 icon:imageC];

[mySingleton.bananas addObject:yellowBanana];
[mySingleton.bananas addObject:greenBanana];
[mySingleton.bananas addObject:rottenBanana];
}


解决方案

  @interface Singleton:NSObject 
@property(nonatomic,retain)NSMutableArray *
+(Singleton *)singleton;
@end
@implementation Singleton
@synthesize bananas;
+(Singleton *)singleton {
static dispatch_once_t pred;
static Singleton * shared = nil;
dispatch_once(& pred,^ {
shared = [[Singleton alloc] init];
shared.bananas = [[NSMutableArray alloc] init];
});
return shared;
}
@end

singleton在您第一次使用时。您可以随时随地调用它:

  NSLog(@%@,[Singleton singleton] .bananas); 


It's a classic problem.

I would like to access an array of objects from anywhere within my app. I would also like to do this using a singleton. My questions are:

  1. Where do I instantiate my singleton object?
  2. Where do I instantiate my NSMutable array of objects?
  3. How do I refer to this array from anywhere within my project?

All code and examples are greatly appreciated!

EDIT 1

This is what I have so far. I can't figure out though how to access this array of bananas correctly and consistently:

#import <Foundation/Foundation.h>

@interface Singleton : NSObject {
    NSMutableArray *bananas;
}

@property (nonatomic, retain) NSMutableArray *bananas;

@end


#import "Singleton.h"

static Singleton *mySingleton;

@implementation Singleton

@synthesize bananas;

#pragma mark SingletonDescption stuff

+ (Singleton *)mySingleton
{
    if(!mySingleton){
        mySingleton = [[Singleton alloc]init];
    }

    return mySingleton;
}

+ (id)allocWithZone:(NSZone *)zone
{
    if (!mySingleton) {
        mySingleton = [super allocWithZone:zone];
        return mySingleton;
    } else {
        return nil;
    }
}

- (id)copyWithZone:(NSZone*) zone
{
    return self;
}

- (void)release
{
    // NO OP
}

@end

EDIT 2

This is how I'm trying to use my singleton object to have an array of objects placed in a table cell. Nothing is happening and the table cell comes up blank :(

- (id)init
{
    [super initWithStyle:UITableViewStylePlain];

    // bananas = [[NSMutableArray alloc] init];

    Singleton *mySingleton = [[Singleton alloc]init];
    mySingleton.bananas = [[NSMutableArray alloc]init];

    UIImage *imageA = [UIImage imageNamed:@"A.png"];
    UIImage *imageB = [UIImage imageNamed:@"B.png"];
    UIImage *imageC = [UIImage imageNamed:@"C.png"];

    Banana *yellowBanana = [[Banana alloc] initWithName:@"Yellow" description:@"Beautiful" weight:22.0 icon:imageA];
    Banana *greenBanana =  [[Banana alloc] initWithName:@"Green" description:@"Gorgeous" weight:12.0 icon:imageB];
    Banana *rottenBanana = [[Banana alloc] initWithName:@"Rotten" description:@"Ugly" weight:8.0 icon:imageC];

    [mySingleton.bananas addObject:yellowBanana];
    [mySingleton.bananas addObject:greenBanana];
    [mySingleton.bananas addObject:rottenBanana];
}

解决方案

Do your singleton like this:

@interface Singleton : NSObject
@property (nonatomic, retain) NSMutableArray *bananas;
+(Singleton*)singleton;
@end
@implementation Singleton
@synthesize bananas;
+(Singleton *)singleton {
    static dispatch_once_t pred;
    static Singleton *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[Singleton alloc] init];
        shared.bananas = [[NSMutableArray alloc]init];
    });
    return shared;
}
@end

The singleton is initialized the first time you use it. You can call it from anywhere at any time:

NSLog(@"%@",[Singleton singleton].bananas);

这篇关于使用单例创建可以由多个视图访问的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆