In my Swift code, I am trying to instantiate an Objective-C view controller like below, however the viewDidLoad
method is not being called.
let detailMessageVC = DetailMessageViewController(nibName:nil, bundle:nil)
If I instantiate the same viewcontroller from Objective-C code like this it does get called. There is no xib for the view controller.
self.detailMVC = [[DetailMessageViewController alloc] init];
Does anyone know why this is happening? I would like to have the same behaviour when calling from Swift as I rely on the viewDidLoad
to perform various initialisation. This is with Xcode 6.3.2
As comment stated, there is no link between initialisation of a view controller and it's viewDidLoad
callback.
You can even have viewDidLoad
called before init. Because viewDidLoad
is called if the ViewController effectively loaded a view.
Check there : https://stackoverflow.com/a/4862772/1486230
Although, I wouldn't rely on viewDidLoad
unless you know that you will effectively display your ViewController; Mostly by using myVC.view
.
Just think like initialising a ViewController (which is only a logic object) is different than creating its view. For performance reason, iOS won't create the graphic object if they are not needed. If you get a difference between Swift and Objective-C, it's more because Swift is probably better on optimisation. But the behaviour is normal.
对于 iOS 9+,您可以调用vc.loadViewIfNeeded()
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.