Aeradriel
Aeradriel

Reputation: 1232

Could not load Nib in bundle - Wrong nibname in error

I'm trying to push a ViewController's view initialized with a xib file. To do so, I'm calling the initializer of my controller which calls himself the initwithnibname:bundle: to load the correct xib file.The problem is that I'm getting the following error

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: [...] with name 'BYZ-38-t0r-view-x4c-fw-L1g'

The nib name in the error does not match the provided nib name in the initializer call.

Here is my code :

ViewController declaration/initialization

    let connexionViewController = ConnexionViewController()

    self.view.addSubview(connexionViewController.view) // Exception thrown on this line

ViewController code

import Foundation
import UIKit

class ConnexionViewController: UIViewController {

    @IBOutlet var validateButton: UIButton!
    @IBOutlet var usernameTextField: UITextField!

    required init(coder: NSCoder) {
        super.init(coder: coder)
    }

    override init() {
        super.init(nibName: "ConnexionViewController", bundle: nil)
    }

    override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

The xib file's name match the string provided to the initializer and the xib is added to the build phases for the actual target.

Upvotes: 0

Views: 5110

Answers (2)

jeet.chanchawat
jeet.chanchawat

Reputation: 2595

ViewController * pvc = [[ViewController alloc]initWithNibName:NSStringFromClass([ViewController class]) bundle:nil];

I was loading nib this way to avoid strings. This in turn started returning nib name as myproject.ViewController on adding swift compatibility to existing objC project for Mix and Match.

To sort this, either use the below kind of code or do string processig to crop the string till .

ViewController * pvc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];

Hope this helps someone in future.

Upvotes: 2

Aeradriel
Aeradriel

Reputation: 1232

It seems that the navigation controller wasn't fully initialized when the new controller was pushed. I changed the location of the call and it now works fine.

Upvotes: 1

Related Questions