I have a Storyboard and instantiate a view controller from code like this:
NSLog(@"1");
MyController *vc = (MyController *)[[UIStoryboard storyBoardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"MyController"];
NSLog(@"4");
The implementation of the MyController class includes the following:
- (void)viewDidLoad {
NSLog(@"2");
[super viewDidLoad];
[NSThread sleepForTimeInterval:2];
NSLog(@"3");
}
I expect to see the following output when I run my code:
1
2
3
4
But it seems that -viewDidLoad
is not even called until I access MyController
's view:
1
4
If I modify the first like this:
NSLog(@"1");
MyController *vc = (MyController *)[[UIStoryboard storyBoardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"MyController"];
[vc view]; // Just accessing
NSLog(@"4");
Then it the output will be:
1
4
2
3
How can I be sure that my properties are initialized (-viewDidLoad
ran) and I can give parameters to my controller? What is the recommended way to do this?