I've been going through some iOS examples online I came across an app where the app initializes an array and then add objects to it when the iOS app launches. Initializing works when I use (void) viewDidLoad
in my implementation file but initialing the array does not work when I use
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
Can someone tell me why is that so? Thanks!
Here is the code -
(void) viewDidLoad
{
if (self) {
questions = [[NSMutableArray alloc] init];
answers = [[NSMutableArray alloc] init];
// Add objects to the arrays
[questions addObject:@"What is 1+1"];
[answers addObject:@"2"];
[questions addObject:@"What is 2+2"];
[answers addObject:@"4"];
[questions addObject:@"What is 3+3"];
[answers addObject:@"6"];
}
[super viewDidLoad];
and the code for initWithNibName
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Create two arrays and make the pointers point to them
questions = [NSMutableArray array];
answers = [NSMutableArray array];
// Add questions and answers to the arrays
[questions addObject:@"From what is cognac made?"];
[answers addObject:@"Grapes"];
[questions addObject:@"What is 7 + 7?"];
[answers addObject:@"14"];
[questions addObject:@"What is the capital of Vermont?"];
[answers addObject:@"Montpelier"];
}
return self;
}
I think that when you use a storyboard, initWithCoder is called instead of initWithNibName:bundle:, so put the array initialization in there instead.