Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS5: Confusion with loadview and init and instance variables

I'm new to iOS5 and storyboarding.

I noticed that if I declare an instance variables inside my viewcontroller .h file and set the values inside my init of .m file of my viewcontroller, when the view of the viewcontroller is displayed, my instance variables show null inside viewDidLoad. In order for me to get myvariables, I'd need to do [self init] inside viewDidLoad. My questions are:

@interface tableViewController : UITableViewController
{
NSMutableArray *myvariable;
}
@end

@implementation tableViewController
-(id)init
{
myvariable = [[NSMutableArray alloc]initWithObjects:@"Hi2",@"Yo2",@"whatsup2", nil];
}

- (void)viewDidLoad
{
NSLog(@"%@",myvariable); // DISPLAYS NULL
[super viewDidLoad];
}
  1. Why isn't my variables available in viewdidLoad when I declared and implemented in my .h and .m files?
  2. If that's the case, is viewDidLoad or viewWillAppear the common places to load the data for the viewcontroller?
  3. It looks like even when you instantiate a viewcontroller and the init function gets called, the viewDidLoad doesn't necessarily have the variables to be displayed.
  4. Where's the right place/methods to load the model(data) for my viewcontroller?

Thanks in advance

like image 638
sonoluminescence Avatar asked Nov 13 '11 01:11

sonoluminescence


1 Answers

So to answer your first question, the initializer which is called in this case is initWithCoder:, not init. So if you move your NSArray initialization there to initWithCoder: you should find that it is available before your view loads.

Don't forget you must call your superclass' initializer also. So a pattern like this will work:

-(id)initWithCoder:(NSCoder *)aDecoder {

    if ((self = [super initWithCoder:aDecoder])) {

        // initialize what you need here
    }

    return self;

}

You will also get awakeFromNib called after initWithCoder: and after all your outlets have been connected, so if your initialization depends on the outlets being populated, then there is the opportunity to do that initialization there.

And then of course you have viewDidLoad and viewWillAppear:. I don't know that there is a general answer to the "right" method to use (questions 2 and 4). It depends on how much data you have, how often it needs be refreshed, and how long it takes to load. My opinion is that that's a decision to be taken for each case.

For question #3, do you have an example of what you've seen there? The initializer will definitely be called before viewDidLoad. The trick is to know which initializer is being called.

Keep in mind that viewDidLoad may be called multiple times during the lifetime of your view controller. So be prepared for that. And of course viewWillAppear: is even more likely to be called multiple times over the lifetime of your view controller.

Hope that helps.

like image 50
Firoze Lafeer Avatar answered Nov 15 '22 07:11

Firoze Lafeer