无法以编程方式在Swift中创buildUIViewController

当我尝试在Swift创build一个UIViewController的实例时,即使我没有在视图控制器(或任何其他的FWIW)中定义任何指定的inits,所有inheritance的初始化器都是不可用的。

此外,如果我试图通过使其显示根视图控制器,它永远不会显示:

 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 } 

视图控制器的代码只是Xcode的模板:

 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. } */ } 

任何人都知道发生了什么?

正如您已经指出的那样:只要nib名称与objective-C中的类名相匹配,即使在初始化视图控制器时未指定nib名称,视图控制器仍然会查找名称为nib的文件匹配视图控制器类的名称 。

但由于某种原因(也许这是一个错误),这不是在斯威夫特的情况。

而不是写作:

 let vc = ImageViewController() 

初始化视图控制器时,必须明确指定一个接口:

 let vc = ImageViewController(nibName: "nibName", bundle: nil) 

为了方便起见,我提出了这个解决方法,以便我不必在任何地方使用更长的初始化程序。 我有我的所有控制器扩展的BaseViewController

 init () { let className = NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last super.init(nibName: className, bundle: NSBundle(forClass: self.dynamicType)) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } 

请注意,使用NSBundle(forClass: self.dynamicType)负责不同目标中的命名空间。

然后,当我想实例化一个ViewController ,我使用MyViewController() ,你可以在Objective-C中。

 let Vc = UIViewController() vc.view = yourview 

或者将上面的vc添加到View作为ChildviewController

 configureChildViewController(vc,onView:yourview in Viewcontroller) 

使用此扩展名为UiViewController

 extension UIViewController { func configureChildViewController(childController: UIViewController, onView: UIView?) { var holderView = self.view if let onView = onView { holderView = onView } addChildViewController(childController) holderView.addSubview(childController.view) constrainViewEqual(holderView, view: childController.view) childController.didMoveToParentViewController(self) } func scatterChildViewController(childController: UIViewController) { childController.willMoveToParentViewController(self) childController.view.removeFromSuperview() childController.removeFromParentViewController() } func constrainViewEqual(holderView: UIView, view: UIView) { view.translatesAutoresizingMaskIntoConstraints = false //pin 100 points from the top of the super let pinTop = NSLayoutConstraint(item: view, attribute: .Top, relatedBy: .Equal, toItem: holderView, attribute: .Top, multiplier: 1.0, constant: 0) let pinBottom = NSLayoutConstraint(item: view, attribute: .Bottom, relatedBy: .Equal, toItem: holderView, attribute: .Bottom, multiplier: 1.0, constant: 0) let pinLeft = NSLayoutConstraint(item: view, attribute: .Left, relatedBy: .Equal, toItem: holderView, attribute: .Left, multiplier: 1.0, constant: 0) let pinRight = NSLayoutConstraint(item: view, attribute: .Right, relatedBy: .Equal, toItem: holderView, attribute: .Right, multiplier: 1.0, constant: 0) holderView.addConstraints([pinTop, pinBottom, pinLeft, pinRight]) } 

}