I have a litle problem with ARC which I understand why it does what it does but not how to prevent it. This code is part of a sample "Tick tac toe" game.
The problem is allocating a new UIView
inside a loop subclassed with the name Tile
. Once it's setup I will add the Tile
(the UIView
) to the current view and add it to the gamecontroller array of tiles later on used for reference.
Now the problem is that with every iteration of the loop, the tile object(s) get auto released, and I want them to be retained so I can store them in the gamecontroler's tile container. How do I make it remember the tiles?
This is the code on the gameDelegate:
- (void) addTile:(Tile *)tile {
NSLog(@"Add tile %@", self.tiles);
[tiles addObject:tile];
}
The output of the last add is here:
Posted on pastebin.com for better formatting
At this point, as expected, the whole local tiles array inside the game controller will be outputted and that's as expected; there is a list with Tile objects.
This is the code in board.m
(subclass of UIView
).
-(void) drawBoard
{
NSLog(@"Drawboard called");
for (int j =0; j < 3; j++) {
for (int i = 0; i < 3; i++) {
CGRect frame = CGRectMake(tilex, tiley, hlineDistance-1, vlineDistance-1);
Tile* tile = [[Tile alloc] initWithFrame:frame];
[self addSubview:tile];
// ...
[self.gameDelegate addTile:tile];
}
// ...
}
// ...
}