Is this why I use pointers?
I know this is very basic but I need clarification. I'm trying to develop an iPad app but having trouble. My explanation may be a little too much information needed but bear with me.
I have two ViewControllers. One is called NewGameViewController while the other is called GameViewController. In the NewGameViewController I display the GameViewCont开发者_开发问答roller like so:
GameViewController *controller = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
So this creates an instance of GameViewController in memory. GameViewController has some ivars and methods defined in it. One of the ivars is an array that gets filed with seat objects when GameViewController loads (in the viewWillAppear method).
I also have an object called player. This is just a subclass of UIView. When GameViewController loads it adds up to six player objects to the subview. I need each player object to be able to access the same instance in memory of GameViewController that was loaded by NewGameViewController. Would I use a pointer in my player objects to access the same instance of GameViewController? I've found that if I load a new instance of GameViewController into memory and attempt to use it the array ivar tends to be empty and therefore unuseable. How would I be sure that my pointer to GameViewController is pointing to the correct instance of GameViewController?
When GameViewController loads it adds up to six player objects to the subview.
So, here's what you can do. Define a property on Player called containingViewController or gameViewController.
@property (nonatomic, retain) GameViewController *containingViewController;
Don't forget to also @synthesize containingViewController;
in your implementation file (Player.m) and [containingViewController release];
in your dealloc
.
Then, when you initialize the players in the viewWillAppear
method, set the property:
player1.containingViewController = self;
That should take care of all of your issues.
精彩评论