When I try to create an instance of a UIViewController
in Swift
, all the inherited initialisers are unavailable, even though I didn't define any designated inits in the view controller (or anything else, FWIW).
Also, if I try to display it by making it the root view controller, it never gets displayed:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.makeKeyAndVisible()
window?.backgroundColor = UIColor.greenColor()
let vc = ImageViewController()
window?.rootViewController = vc
return true
}
The code for the view controller is just Xcode's template:
import UIKit
class ImageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Anybody knows what's going on????
As you've pointed out: As long as the nib name matches the class name in objective-C, even if you don't specify a nib name when initializing the view controller, the view controller will still look for the nib file whose name matches the name of the view controller class.
But for some reason (perhaps it's a bug), this is not the case in Swift.
Instead of writing:
You have to explicitly specify an interface when initializing the view controller: