Basically, my problem is that I’m trying to create 3 instances of a UIView through a loop. I’m using ARC and I don’t really know if what I want to do is possible with it. Here’s the code I currently have :
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"RoomView"
owner:self
options:nil];
NSMutableArray *roomViews = [[NSMutableArray alloc] initWithCapacity:[gtb.rooms count]];
for (i = 0; i < [gtb.rooms count]; i++)
{
RoomView *rcv = [[RoomView alloc] init];
NSDictionary *room = [gtb.rooms objectAtIndex:i];
rcv = [nibViews objectAtIndex:0];
NSLog(@"Start rcv = %@", rcv);
rcv.roomNumber.text = [NSString stringWithFormat:@"Chambre %d", i + 1];
rcv.roomType.text = [room objectForKey:@"roomType"];
[rcv setFrame:CGRectMake(0, sizeOfContent, rcv.frame.size.width, rcv.frame.size.height)];
sizeOfContent += rcv.frame.size.height;
[roomViews addObject:rcv];
NSLog(@"End rcv = %@", rcv);
}
for (i = 0; i < [gtb.rooms count]; i++)
NSLog(@"Room #%i : %@", i, [roomViews objectAtIndex:i]);
And here’s what I have in the logs :
2012-09-20 10:15:00.287 AppName[2792:707] Start rcv = <RoomView: 0x148570; frame = (0 0; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.289 AppName[2792:707] End rcv = <RoomView: 0x148570; frame = (0 420; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.312 AppName[2792:707] Start rcv = <RoomView: 0x148570; frame = (0 420; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.314 AppName[2792:707] End rcv = <RoomView: 0x148570; frame = (0 527; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.316 AppName[2792:707] Start rcv = <RoomView: 0x148570; frame = (0 527; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.317 AppName[2792:707] End rcv = <RoomView: 0x148570; frame = (0 634; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.319 AppName[2792:707] Room #0 : <RoomView: 0x148570; frame = (0 634; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.323 AppName[2792:707] Room #1 : <RoomView: 0x148570; frame = (0 634; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
2012-09-20 10:15:00.325 AppName[2792:707] Room #2 : <RoomView: 0x148570; frame = (0 634; 320 107); autoresize = W+H; layer = <CALayer: 0x148510>>
I know if I wasn’t using ARC, I should have placed something like [rcv autoRelease] at the end of the loop, but with ARC, I can’t.
Is there any solution to solve it, or must I disable ARC for this file ?
Thanks for your help !
You need to replace this line:
With this one:
Because if not, you just are accessing the same view object, not a new one copied from it (strong reference not copied).
And as a side not, there is no influence of ARC in that matter. You would have same problem without ARC. With ARC you are just not writing release/autorelease etc.