0

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;
}
Sumanth
  • 4,913
  • 1
  • 24
  • 39
wackytacky99
  • 614
  • 1
  • 14
  • 33
  • Do you know whether initWithNibName:bundle: is being called? It may or may not be depending on the structure of your app (like whether you're using a storyboard or not, for instance). Put a log statement in there and see. – rdelmar Jul 26 '12 at 05:17
  • It's not being called. I checked using NSLog. And yes, I'm using Storyboard. Am I supposed to do something difference here to make initWithNibName work? – wackytacky99 Jul 26 '12 at 05:19

1 Answers1

0

I think that when you use a storyboard, initWithCoder is called instead of initWithNibName:bundle:, so put the array initialization in there instead.

rdelmar
  • 103,982
  • 12
  • 207
  • 218