IOS5: confusion with loadview and init and instance variables

I am new to iOS5 and storyboard.

I noticed that if I declare instance variables in my.controller.h and set the values โ€‹โ€‹inside my .m file of my view manager when the viewcontroller is displayed, my instance variables display null inside viewDidLoad. In order for me to get myvariables, I need to do [self init] inside viewDidLoad. My questions:

@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]; } 
  • Why are my variables not available in viewdidLoad when I declared and implemented .h and .m in my files?
  • If this is the case, are viewDidLoad or viewWillAppear common places to load data for the viewcontroller?
  • It appears that even when you instantiate the viewcontroller and call the init function, viewDidLoad does not have to display the variables.
  • Where is the appropriate place / methods for loading the model (data) for my view manager?

Thanks in advance

+6
source share
1 answer

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

Remember that you must also call the initializer of your superclass. Thus, such a template will work:

 -(id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { // initialize what you need here } return self; } 

You will also get awakeFromNib after initWithCoder: and after all your points are connected, so if your initialization depends on the settlements, the ability to do initialization there.

And then, of course, you have viewDidLoad and viewWillAppear:. I do not know that there is a general answer to the โ€œcorrectโ€ method of use (questions 2 and 4). It depends on how much data you have, how often you need to update and how long it takes to download. My opinion is that this decision should be made for each case.

To question number 3, do you have an example of what you saw there? The initializer will definitely be called before viewDidLoad. The trick is to know which initializer is being called.

Keep in mind that viewDidLoad can be called several times during the life of your view controller. So be prepared for this. And, of course, viewWillAppear: it will be called even more often several times during the life of your view controller.

Hope this helps.

+14
source

Source: https://habr.com/ru/post/901364/


All Articles