Reputation: 1177
In my header file I have:
@property (nonatomic, retain) NSMutableArray * array1;
and in My initWithNibName
method I have:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.array1 = [[NSMutableArray alloc] initWithObjects:@"test", nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(DoUpdateLabel:) name:@"DoUpdateLabel" object:nil];
}
return self;
}
The DoUpdateLable
method does some updating to the array (adds a number of items). Once this is complete and ViewDidLoad executes my array disappears and becomes null which is annoying because I want to load it into a table view.
Here's a trimmed down version of the DoUpdateLable
method. I've had to trim it down as the actual method includes a lot of JSON parsing and URL requests:
-(void)DoUpdateLabel:(NSNotification *) notification
{
int count = 0;
while(count < 59)
{
NSString * currentpatname = @"test1";
if(![currentpatname isEqualToString:@""])
{
[self.array1 addObject:currentpatname];
NSLog(@"array: %@", self.array1);
}
count++;
}
NSLog(@"final array: %@", self.array1);
}
I'm a little stuck as to why it's not being retained. Any help would be much appreciated!
Thanks!
Upvotes: 1
Views: 174
Reputation: 104092
initWithNibName:bundle: is called if you just use alloc init somewhere. You probably have done that, but you shouldn't be. Delete that, and put the code you have in your question in initWithCoder:, then it should work properly.
Upvotes: 0
Reputation: 1814
Looking at your updated question, you're setting up that array inside the scope that starts with // Custom initialization
. But what you don't notice is that viewDidLoad
is actually called on this line:
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
That means your array is still nil inside viewDidLoad, and thus DoUpdateLabel
I would recommend instead initializing that array inside -viewDidLoad
instead, and you'll see the results you're looking for.
Upvotes: 2