objective-c - 这就是我使用指针的原因吗?

标签 objective-c ios xcode pointers uiviewcontroller

我知道这是非常基本的,但我需要澄清一下。我正在尝试开发 iPad 应用程序,但遇到了麻烦。我的解释可能需要的信息有点多,但请耐心等待。

我有两个 ViewController。一个叫做 NewGameViewController,另一个叫做 GameViewController。在 NewGameViewController 中,我像这样显示 GameViewController:

GameViewController *controller = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];

所以这会在内存中创建一个 GameViewController 实例。 GameViewController 中定义了一些 ivars 和方法。其中一个 ivar 是一个数组,当 GameViewController 加载时(在 viewWillAppear 方法中),该数组会与座位对象一起归档。

我还有一个对象叫做播放器。这只是 UIView 的一个子类。当 GameViewController 加载时,它会将最多六个玩家对象添加到 subview 。我需要每个玩家对象都能够访问由 NewGameViewController 加载的 GameViewController 内存中的同一个实例。我会在我的播放器对象中使用一个指针来访问同一个 GameViewController 实例吗?我发现如果我将 GameViewController 的新实例加载到内存中并尝试使用它,数组 ivar 往往是空的,因此无法使用。我如何确定指向 GameViewController 的指针指向正确的 GameViewController 实例?

最佳答案

When GameViewController loads it adds up to six player objects to the subview.

那么,您可以执行以下操作。在 Player 上定义一个名为 containingViewController 或 gameViewController 的属性。

@property (nonatomic, retain) GameViewController *containingViewController;

不要忘记 @synthesize containingViewController; 在你的实现文件 (Player.m) 和 [containingViewController release]; 在你的 dealloc.

然后,当您在 viewWillAppear 方法中初始化播放器时,设置属性:

player1.containingViewController = self;

这应该可以解决您的所有问题。

关于objective-c - 这就是我使用指针的原因吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6721308/

相关文章:

c# - 在 Xamarin 中绑定(bind) PayPalMobile iOS 库

iphone - 如何卸载xcode并安装另一个版本?

ios - 实现 KIF 的示例

iphone - 有人有使用 YAJL 的 Objective-C 绑定(bind)的经验吗?

objective-c - 如何在 Mac OS X 中获取相对于窗口的光标位置?

iOS静态库编译错误-目录为空

ios - 在ios中按顺序下载音频文件

iphone - 如何使 Xcode 自动完成委托(delegate)方法?

ios - 错误 - 打开 session 失败,因为未声明协议(protocol) [名称]

xcode - 无效的 info.plist - 将应用程序发送到应用程序商店时的 UIMainStoryboardFile 或 NSMainNibFile