无法以编程方式在Swift中创建UIViewController

时间:2014-11-26 13:56:55

标签: ios cocoa-touch swift uiviewcontroller initializer

当我尝试在UIViewController中创建Swift的实例时,所有继承的初始化程序都不可用,即使我没有在视图控制器中定义任何指定的inits(或任何内容)否则,FWIW)。

此外,如果我尝试通过将其设置为根视图控制器来显示它,它将永远不会显示:

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

}

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

3 个答案:

答案 0 :(得分:7)

正如您所指出的:只要nib名称与objective-C中的类名匹配,即使您在初始化视图控制器时没有指定nib名称,视图控制器仍然会look for the nib file whose name matches the name of the view controller class

但出于某种原因(也许这是一个错误),Swift就不是这样了。

而不是写作:

let vc = ImageViewController()

初始化视图控制器时必须显式指定接口:

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

答案 1 :(得分:3)

为方便起见,我想出了这个解决方法,以便我不必在任何地方使用更长的初始化程序。我有一个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时,我会在Objective-C中使用MyViewController()

答案 2 :(得分:0)

let Vc = UIViewController()
vc.view = yourview

或将以上vc添加到View for 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])
}

}