Reputation: 162
I want to test the difference between viewDidLoad and loadView ,so i create a single view application and than set the didFinishLaunchingWithOptions as flows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
viewController = [[MyViewController alloc] init];
// viewController.view.backgroundColor = [UIColor redColor];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
In my MyViewController's m file is as flows:
#import "MyViewController.h"
@interface MyViewController ()
@end
@implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",self.view);
self.view.backgroundColor = [UIColor redColor];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadView {
self.view = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
}
@end
But the loadView method or viewDidLoad are not called.Why? But when i cancel the annotation at the didFinishLaunchingWithOptions .they all are called.
Upvotes: 0
Views: 110
Reputation: 10479
Why the viewDidLoad is not called?
This behavior happens because your window is a nil. So your controller have not displayed and its view is not loaded.
Try this:
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
ViewController *viewController = [[ViewController alloc] init];
// viewController.view.backgroundColor = [UIColor redColor];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
Why the viewDidLoad is called if uncommented this line
viewController.view.backgroundColor = [UIColor redColor];
?
A view of an UIViewController
is lazy loading. If you cancel the annotation at the didFinishLaunchingWithOptions
, although your controller have not displayed, but you have visit its view
property then the view will to be loaded then going to be discarded.
Upvotes: 1
Reputation: 1157
"loadView" is called when self.view is called and self.view is nil, so if you uncomment
viewController.view.backgroundColor = [UIColor redColor];
you literally call self.view, it would then call(internally) "loadView" and then "viewDidLoad".
I guess it is because "self.window" is not initialised, "loadView" and "viewDidLoad" are not called.
Upvotes: 1
Reputation: 4046
I just checked with simple example:
@implementation TestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"test");
}
- (void)loadView
{
NSLog(@"load");
}
@end
And it works fine:
2015-08-16 13:18:46.163 Example[51603:406197] load
2015-08-16 13:18:46.164 Example[51603:406197] test
2015-08-16 13:18:46.164 Example[51603:406197] load
2015-08-16 13:18:46.164 Example[51603:406197] test
Here is delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = [TestViewController new];
[self.window makeKeyAndVisible];
return YES;
}
Update #1
I forgot about default storyboard I have. I don't use it but it is required for window
property initialisation in delegate:
Implementation of this property is required if your app’s Info.plist file contains the UIMainStoryboardFile key. Fortunately, the Xcode project templates usually include a synthesized declaration of the property automatically for the app delegate. The default value of this synthesized property is nil, which causes the app to create a generic UIWindow object and assign it to the property. If you want to provide a custom window for your app, you must implement the getter method of this property and use it to create and return your custom window.
Upvotes: 1