1
votes

I am hoping that someone else has run into this problem before.

In an Xcode Cocoa Application project I create a *.xib file with a single view, and some random sub views. I create a *ViewController.m and inherit from NSViewController. I set the file owner in the *.xib to my controller and set the outlet view to the main view. I then create a new controller instance in the AppDelegate's applicationDidFinishLaunching: and add its view as a child to self.window.contentView.

Here is the weird part... this works JUST fine in a fresh Xcode Cocoa project. The view from the *.xib is added to the main window as expected. However in the project I am currently working on I get [NSViewController loadView] loaded the "(null)" nib but no view was set no matter what I do. At one point in time this worked in this project, it now errors every time. I have even reverted the entire repo from an old Git commit! I assume i'm seeing an Xcode bug?

EDIT More info: This seems to be connected to having the project as a child of another Xcode project. If I open the project individually (Rather than the parent project) it runs as expected.

3
my guess is that your nib wasn't added to the target you were building.Grady Player
Are nibs added to a target? The controllers were for sure added under "Compile Sources".Alec Thilenius
yeah Nibs are absolutely added to targets, they get added to the resources in your bundle.Grady Player

3 Answers

7
votes

I had a similar problem: I wrote a ViewController then added a xib and changed the File's Owner there. Then I added a ViewController object in my MainMenu.xib and changed the Custom Class of this object to my ViewController.
What I forgot and what caused the same exception: In the MainMenu.xib I had to click on the ViewController object and in the Attributes Inspector I needed to specify the Nib Name which should be loaded when the ViewController gets created. Sadly Xcode needed a restart for getting my changes.
Anyway, I hope this helps everyone who checked every view connection twice but still got the exception.

0
votes

Although not an explanation of why it was happening, I found out how to fix this. I changed the entire project over to a workspace and my runtime error vanished. Originally it was a parent project, with 2 children projects.

0
votes

Well knowing well that this code SHOULD work. I did a simple experiment. I simply recreated the xib for that particular item and the issue went away.

So IMHO this is a bug from apple. I sure have a long list of them piling up here and there. ...

This whole thing started after i noticed very slow performance on the NSCollectionView, originally i had the cells into their own nib, but it appears this is not needed anymore, since you can define the custom NSCollectionViewItem in the same xib as the NSCollectionView. Cocoa will then call a copyWithZone: and will be fine.

I guess my fault for not having adapted to some of the new patters from Apple.