user3033437 user3033437 - 6 months ago 35
Swift Question

How to set xib file as initialViewController?

since I've been unable to do even one succesfull merge with storyboard, I'm trying to switch to xib files. I'm trying to set one xib file as initialViewController as you do in storyboard, so first I've disabled that property in storyboard. Following some examples from google, I've made an extension that is supposed to return a xib file:

extension UIViewController {
class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> UIViewController? {
return UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(nil, options: nil)[0] as? UIViewController
}


and then called that class function from the app delegate and try to load the xib file as root view controller like this:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.


let mainView = UIViewController.loadFromNibNamed("MainView")
//window?.addSubview(mainView!)
self.window?.rootViewController = mainView

return true
}


My xib file name is MainView.xib, I have a UIView and a MainViewController on it, MainViewController inherits from ViewController and it's still empty:

enter image description here

enter image description here

enter image description here

Whatever I try I allways get the same error message:

Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?

Answer

You still have the project set up to use the Main.storyboard file. In the project / general tab, you need to clear this section:

enter image description here

If you do this you will also need to create the UIWindow yourself and set the app delegate's window property, as this is not created if you don't use a storyboard. See How to create an Empty Application in Xcode 6 without Storyboard for details.

Comments