I have been attempting to have a object that I can use across multiple view controllers by following this thread.
我一直试图通过遵循这个线程来拥有一个可以跨多个视图控制器使用的对象。
One instance across multiple views in Cocoa Touch
Cocoa Touch中多个视图中的一个实例
But it has not been working for me. So I started with the basics to see what was going on. I created a local instance of the object.
但它并没有为我工作。所以我从基础开始,看看发生了什么。我创建了一个对象的本地实例。
PlayerData *playerOne = [[PlayerData alloc] init];
playerOne.completedRound += 1;
I can inspect this in the debugger and I see 0 for all values when I create it and then it gets updated by the appropriate line of code so I feel like my object class is written correctly.
我可以在调试器中检查这个,当我创建它时,我看到所有值的0,然后通过适当的代码行更新,所以我觉得我的对象类是正确编写的。
When I try to define the object in my header file to like this:
当我尝试在头文件中定义对象时,我喜欢这样:
In my UIViewController.h I added the following
在我的UIViewController.h中,我添加了以下内容
#import "PlayerData.h"
PlayerData *playerOne;
@property (nonatomic, retain) PlayerData *playerOne;
In my UIViewContoller.m I have added the following
在我的UIViewContoller.m中,我添加了以下内容
#import "PlayerData.h"
@synthesize playerOne;
playerOne.completedRound += 1;
I cannot get it to work. The code compiles fine but viewing the instance in the debugger non of the variable ever get set.
我无法让它发挥作用。代码编译得很好,但是在调试器中查看实例,而不是变量设置。
1 个解决方案
#1
Couple of possibly silly questions:
几个可能很愚蠢的问题:
- Does
[PlayerData init]
initialise your property to zero? - Does your
UIViewController
alloc
/init
thePlayerData
object before you try to increment it?
[PlayerData init]是否将您的属性初始化为零?
在尝试递增之前,您的UIViewController是否会分配/初始化PlayerData对象?
#1
Couple of possibly silly questions:
几个可能很愚蠢的问题:
- Does
[PlayerData init]
initialise your property to zero? - Does your
UIViewController
alloc
/init
thePlayerData
object before you try to increment it?
[PlayerData init]是否将您的属性初始化为零?
在尝试递增之前,您的UIViewController是否会分配/初始化PlayerData对象?