gpt4 book ai didi

ios - 单例不会跨 View Controller /spritekit 场景持续存在

转载 作者:行者123 更新时间:2023-11-29 10:47:03 32 4
gpt4 key购买 nike

这是一个 SpriteKit 游戏。

我正在使用单例来存储单个“显示”对象,以便在整个游戏中访问。 “Show”类有一个 NSString 属性“showTitle”。

在 ViewController1 中,我设置了单例的“显示”属性。为了测试.. 然后我从单例中打印出 'Show' (showTitle) 的字符串属性,它正确地打印了字符串。

在转到 ViewController2 之后,我再次从单例中打印出“Show”(showTitle) 的相同字符串属性,它再次正确打印字符串。

然后,spritekit 场景从 ViewController2 初始化。我尝试打印相同的字符串属性 来自单例的 'Show',它打印 null 而不是字符串。我走得更远,转到 ViewController3,试图从单例中打印 showTitle ..... NULL。

为什么我可以在 ViewControllers 1 和 2 中访问单例的“Show”属性,但不能从 sprite kit 场景或 ViewController3 中访问。我哪里错了?

ShowSingleton.h

#import <Foundation/Foundation.h>
#import "Show.h"

@interface ShowSingleton : NSObject

@property (nonatomic, strong) Show* currentShow;

+(ShowSingleton *)singleton;

@end

ShowSingleton.m

#import "ShowSingleton.h"

@implementation ShowSingleton

@synthesize currentShow;

+(ShowSingleton *)singleton {
static dispatch_once_t pred;
static ShowSingleton *shared = nil;

dispatch_once(&pred, ^{
shared = [[ShowSingleton alloc] init];
});

return shared; }

@end

View Controller 1:

- (IBAction)openShow:(UIButton*)sender
{
//showsarray is an array of 'Show' objects retrieved through core data in another class
[ShowSingleton singleton].currentShow = [showsarray objectAtIndex:sender.tag];

NSLog(@"Opened show: %@", [ShowSingleton singleton].currentShow.showTitle);
//The above line correctly prints the selected showTitle from the singleton
}

openShow: 完成后,segue 打开 ViewController2。 ViewController2 初始化 SpriteKit 场景。

ViewController2:

- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];

SKView * skView = (SKView *)self.view;
if (!skView.scene)
{
skView.showsFPS = YES;
skView.showsNodeCount = YES;
SKScene * scene = [iMarchMyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
}
}

- (void)viewDidLoad
{
//The following line correctly prints the showTitle from ViewController2
NSLog(@"processing show: %@", [ShowSingleton singleton].currentShow.showTitle);
}

我的场景.m:

-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
if ([ShowSingleton singleton].currentShow)
{
//This always gets called, which tells me the object exists, but null is printed for showTitle
NSLog(@"show title from scene: %@", [ShowSingleton singleton].currentShow.showTitle);
}
}
return self;
}

最佳答案

考虑使用 this单例实现:

// ShowSingleton.h

#import <Foundation/Foundation.h>
#import "Show.h"

@interface ShowSingleton : NSObject

+ (instancetype)singleton;

@end

// ShowSingleton.m

#import "ShowSingleton.h"

@implementation ShowSingleton

+ (instancetype)singleton {
static id sharedInstance = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});

return sharedInstance;
}

@end

关于ios - 单例不会跨 View Controller /spritekit 场景持续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22029777/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com