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];
}
Thanks in advance
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With