Loaded nib but the 'view' outlet was not s

2018-12-31 22:58发布

I added a new nib file to my project, and tried to load it.

However, when I click on the toolbar icon that is supposed to take me to the view that I created, I get an NSInternalInconsistencyException with the message:

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

So I opened up my nib file, and I see for the view that there are no referencing outlets set. However, I try to click and drag the circle for "new referencing outlet" to File's Owner, but it won't let me...what do I need to do to get my view to display?

27条回答
谁念西风独自凉
2楼-- · 2018-12-31 23:35

The answer to problem for me was to make sure xib file name and swift file name should not be same. As when creating object of class it was giving priority to xib file and in that case XIB loading was failing to find View outlet.

Example : I was having DataFragement.xib and DataFragment.swift

and I was trying to create object like

let fragment = DataFragment()

to to solve this issue I renamed my xib file to data_fragment.xib and voila everything worked charm.

查看更多
不流泪的眼
3楼-- · 2018-12-31 23:35

To anyone that is using an xib method to create a UIView and having this problem, you will notice that you won't have the "view" outlet under the connections inspector menu. But if you set the File's Owners custom class to a UIViewController and then you will see the "view" outlet, which you can just CMND connect an outlet to the CustomView.

查看更多
与风俱净
4楼-- · 2018-12-31 23:36

The View Identity - Class Identity was not set. After setting it to the appropriate class, the issue was resolved.

查看更多
深知你不懂我心
5楼-- · 2018-12-31 23:36

Are you sure you have a UIView (or subclass) assigned to the "view" property of yourViewController?

Right click on "File Owner" in the left pane of the xib for yourViewController and verify that the "view" outlet is set. If not, set it to a view!

this will definetly fix the Issue

查看更多
不再属于我。
6楼-- · 2018-12-31 23:37

For me, the problem was caused by calling initWithNibName:bundle:. I am using table view cells from a nib file to define entry forms that sit on tableViews. As I don't have a view, doesn't make sense to hook to one. Instead, if I call the initWithStyle: method instead, and from within there, I load the nib file, then things work as expected.

查看更多
旧时光的记忆
7楼-- · 2018-12-31 23:37

I had the same problem, but a different solution was called for. The problem in this case was the class of the File Owner was not connected to xib file.

查看更多
登录 后发表回答