Storyboard returns new instance with old property instances iphone
So i was working on storyboard and i ran into a corner. I created an int
array int[3] and i set values to 0. Then i changed first value on first
page, and second value on another page. I thought both pages has same
instance of int array but i noticed that the array has same values in both
views and same memory address.
So i made stuff more complicated. I created an NSMutableArray, on
viewDidLoad i set this array to nil, then i alloc and init it. I then add
one object to it. Next page - new array! New memory address, no elements.
So i add one element to this array - bam! Address is now the same as in
old view and there are two elements (old one and new one).
Yes i did release that array on dealloc on old view.
So i created static int, and incremented it in viewDidLoad
static int i = 0; i++;
Every time i open new page, with different memory address i get integer
incremented by one and printed out. This happens on empty project with
nothing altered.
My question: Why do i read in apple docs that storyboard creates new and
unique instance of views and then i see this "old value issue"?
UPDATE
Okay so i declare my array like this:
@property (retain, nonatomic) NSMutableArray *destroyedObjects;
Dealloc method:
- (void)dealloc
{
[_destroyedObjects release];
_destroyedObjects = nil;
[super dealloc];
}
Initialisation of the view itself:
- (DataViewController *)viewControllerAtIndex:(NSUInteger)index
storyboard:(UIStoryboard *)storyboard
{
// Return the data view controller for the given index.
if (([self.pageData count] == 0) || (index >= [self.pageData count])) {
return nil;
}
// Create a new view controller and pass suitable data.
DataViewController *dataViewController = [storyboard
instantiateViewControllerWithIdentifier:@"DataViewController"];
return dataViewController;
}
View did load of this controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_destroyedObjects = nil;
_destroyedObjects = [NSMutableArray new];
}
Somwhere within the code i do this (this is done as soon as button in xib
is pressed):
[_destroyedObjects addObject:[NSNumber numberWithInt:[objectView
typeInteger]]];
How i see that values are bad? This is log when i init the view (NSLog
self) and when i set the value (log self and array):
2013-09-12 17:36:02.334 TAApp[29041:c07] <DataViewController: 0x9347fa0>
2013-09-12 17:36:04.731 TAApp[29041:c07] <DataViewController: 0x9336cd0>
2013-09-12 17:36:04.731 TAApp[29041:c07] (
0,
1
)
2013-09-12 17:36:05.671 TAApp[29041:c07] <DataViewController: 0x912bd40>
2013-09-12 17:36:08.100 TAApp[29041:c07] <DataViewController: 0x9336cd0>
2013-09-12 17:36:08.101 TAApp[29041:c07] (
0,
1,
0
)
Both prints made on different pages.
No comments:
Post a Comment