简体   繁体   中英

Swift init ViewController from Xib file error

Using Xcode 8.2 and Swift 2.3

I have a view controller files namVcc.swift and namVccUI.xib

I am trying to initialize the view controller from xib file but getting error

|*| Try 1 :

let namVccVar = UINib(nibName: "namVccUI", bundle:
    nil).instantiateWithOwner(nil, options: nil)[0] as! namVcc

navigationController?.pushViewController(namVccVar, animated: true)

|*| Try 2 :

let namVccVar = namVcc(nibName: "namVccUI", bundle: nil)

navigationController?.pushViewController(namVccVar, animated: true)

Error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'namVccUI'

I also checked the properties of xib file > Target Membership and it's checked.

What else could be the problem?

I am getting a similar but slightly differently worded error when I try to set this up. I get

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "namVccUI" nib but the view outlet was not set.'

when I just create an empty project with your code in Try 2 and a new, clean .xib file. I had to explicitly set the Referencing Outlet of the UIView in the .xib file to the File's Owner and I had to ensure that the Custom Class of the File's Owner in the .xib was set to namVcc. I never could get Try 1 to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM