-1

UPDATE : Answer

I posted my answer below which solved this problem for me. Reason for the issue is entirely different to the post that has been suggested.

Original Question:

I am trying to load a UIView from a xib file which is defined as a ViewController.

        if let overlayView = (Bundle.main.loadNibNamed("CalendarViewController", owner: self, options: nil)?.first as? JTAppleCalendarView) {
        self.navigationController?.view.addSubview(overlayView)
    }

However, every time I run the project, I get a unknown key exception.

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MyApp.ViewController 0x7feac8607cc0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key calendarView.'

I have one view in the xib file which I am connecting to its owner (viewcontroller) through a IBOutlet. This key is given as the one that is causing the error. However, when I checked the UIView's referencing outlets, they are in good standing.

I tried removing the reference and re-adding it and also trying to load the xib by specifying the owner to the xib's view controller. Still no luck.

Update: References

enter image description here

Also the connections portion of the xib's xml:

            <connections>
            <outlet property="calendarView" destination="dqi-m5-FXD" id="mAV-5Y-SXl"/>
        </connections>

Related code:

https://github.com/cooderatgit/CalendarApp

c00der
  • 543
  • 1
  • 4
  • 20
  • Possible duplicate of [What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"](http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key-v) – dan Mar 03 '17 at 18:27
  • I went through that post. However, non of the solutions given there helped me resolve the problem – c00der Mar 03 '17 at 18:35

3 Answers3

2

You are getting this error because of a broken IBOutlet link. Go to outlet tab in xib/storyboard for the selected view controller and look for the outlet connection with '!', remove that and build.

Jitendra Singh
  • 2,103
  • 3
  • 17
  • 26
  • Thanks. I checked for the consistency of the outlets (there's only one outlet), but, I couldn't see any problem there. I will update my question with a screenshot. – c00der Mar 03 '17 at 18:34
  • This error appear because of invalid outlet connection. I would suggest to disconnect and reconnect the outlet if this does not work the delete the view from main view and then add it again. Hope this helps! – Jitendra Singh Mar 03 '17 at 18:50
  • Sure. Thanks. I will give that option a go. – c00der Mar 03 '17 at 19:04
  • If it does not work and if you share the code for related files, I can try to see and find out the issue. – Jitendra Singh Mar 03 '17 at 19:06
  • Thanks. I will try it out and post the complete code for the related files. – c00der Mar 03 '17 at 19:20
  • Hi Jitendra, removing and adding even the entire xib and recreating the references still didn't work. I linked the files in my question. Great if you could take a look. Thanks – c00der Mar 03 '17 at 20:29
  • Seems like you misconfigured the pod 'JTAppleCalendar'. Please review your implementation once again and follow the documentation available here. http://cocoadocs.org/docsets/JTAppleCalendar/2.1.2/index.html – Jitendra Singh Mar 06 '17 at 13:08
1

Try this:

let overlayView = (Bundle.main.loadNibNamed("CalendarViewController", owner: self, options: nil)?.first as? CalendarViewController
    self.navigationController?.view.addSubview(overlayView)
Mannopson
  • 2,634
  • 1
  • 16
  • 32
0

The approach to resolving this issue is as follows:

  1. Create a UIViewController with xib option checked
  2. Go to the xib file and delete the root view that is presented
  3. Add a UIViewController from the object library to the IB. Now, the root of the xib is a UIViewController (not the default view generated by XCode)
  4. Go to the identity inspector of the newly added view controller and change the custom class to the view controller swift file that was created (this is where I went wrong earlier, I was trying to assign a ViewController as the custom class for a view that is created by XCode by default)
  5. Then establish the outlet connections between the ViewController source and the views.

This solved the problem for me. Looking back, this approach makes sense. As pointed out by the other answers, in fact, there was a problem with the reference connections. However, that was happening due to an entirely different reason than, for example, accidentally deleting a reference connection. XCode being unable to establish connections between a view and a view controller that are not associated with each other through the custom class setting. Hope this will help anybody who's trying to load a custom view controller from a xib to another view controller.

c00der
  • 543
  • 1
  • 4
  • 20