Max
Max

Reputation: 809

Unknown class in interface builder

I know that there are a lot of questions on this subject but I've tried most of them to try and figure it out with no success.

the problem: at first I couldn't add my class from the assistant editor cause the custom class didn't pick it up.

  1. I made sure the calls inherits from uiviewcontroller
  2. I made sure the class is added to the target
  3. I tried to remove the class file and re-add it back
  4. finally I've added the class through the storyboard xml and it appeared in the custom class

Now I'm trying to connect my buttons, views etc to its outlets. It seems like they are connected but when running the app and opening that view controller the app crashes and the debugger prints: "Unknown class xxx in Interface Builder file." and than "this class is not key value coding-compliant for the key btnMenu."

I've checked:

  1. That connection inspector is properly set (no error in the outlets there)
  2. Tried to delete derived data
  3. Clean project
  4. Re-install the app

I even tried restarting my mac and of course no success.

can anyone shad some light here? thanks.

Upvotes: 48

Views: 41724

Answers (13)

PBE_90
PBE_90

Reputation: 1

I found the problem for me was that the UIViewController that I was pointing to did not have a ViewController.swift file attached to its "Custom Class" parameter!

Upvotes: 0

nsinvocation
nsinvocation

Reputation: 7637

For those who encounter this problem but with a system class like PKCanvasView or ARView, in a storyboard.

After you've set view's Custom Class to any of those system classes, make sure the Module is set to None. Next go to target's Link Binary with Libraries in Build Phases and manually link the framework which contains that class. One last step is to do a clean and remove derived data (it's critical otherwise the problem won't float away).

Upvotes: 2

Shihab Uddin
Shihab Uddin

Reputation: 6931

After I checked the tick mark called "Inherit Module From Target" in Custom Class in View Controller Section, it works fine. Can Follow the image.

To find this > Select your view controller yellow button > then click "Show Identity Inspector" > then checked the tick mark called "Inherit Module From Target"

enter image description here

Upvotes: 24

Anthony Scott
Anthony Scott

Reputation: 1341

I just had this happen with a UIViewController subclass (with Xcode 9 beta 2 & Swift 4) and the solution was to tick 'Inherit from Target' where I set the Custom Class in IB.

If your view happens to be in a .bundle file (such as for a framework/static library) that gets copied to a different target, you'll want to set an explicit module target for each xib/storyboard class rather than having it inherit from the target that's hosting it.

Upvotes: 98

sibin
sibin

Reputation: 47

none of this helped. My problem was I created custom storyboards and added custom classes. But I forgot to take of the initial view controller arrow from auto-generated main storyboard and changed the initial storyboard to my custom storyboard which fixed it for me.

Upvotes: 0

Ben
Ben

Reputation: 2081

I had this problem with a custom UIView I was testing, where the UIView was in a framework. I had created a simple app, created a UIView in the Storyboard's default ViewController and set its class to MyCustomView. I always got the "Unknown class in interface builder" error when the app launched. I was importing my framework, and I checked in the built product that the framework was there.

The problem was that my test app code never actually used the framework. Even though the Storyboard referenced it, I guess it never got loaded. When I added a [MyCustomView load]; into the test app code, it all worked.

Upvotes: 2

jhonkaman
jhonkaman

Reputation: 555

I ran into this error because I accidentally saved my class file to the Base.lproj folder when I created it.

I fixed the error by right-clicking the file in the Project Navigator and deleting the reference to it. I then moved the file through Finder to the correct folder. Then I right-clicked the main group in the Project Navigator and clicked Add Files to "GroupName"... and selected the class and clicked the Add button.

After that the error went away.

Upvotes: 1

travdu
travdu

Reputation: 331

What @Anthony Scott mentioned is true until you have Framework_A dependent on Framework_B and class is from Framework_B :) Then you need to deselect checkbox and provide Module which contains given class. Thanks for the question btw. I am using Commons framework for other frameworks and it helped me to realize this ;) For those who develop custom Cocoa Touch Frameworks this can be helpful.

Upvotes: 3

Student
Student

Reputation: 423

You need to instantiate the view controller with the custom class. Without that you might have the error.

self.storyboard!.instantiateViewController(withIdentifier: "namePage") as! EmailAndPassword

Upvotes: 0

Mo Iisa
Mo Iisa

Reputation: 504

Check the name of your class. Make sure that it corresponds to the class to which you have assigned your ViewController in the property inspector.

Upvotes: 0

dmaulikr
dmaulikr

Reputation: 468

For me the problem was that the class was not part of the Target Membership. Just add the class to the target, and you should see it back on the interface builder. This Helped in my case.

enter image description here

Upvotes: 28

Natasha
Natasha

Reputation: 6893

If you are trying to assign a class to a ViewController, please make sure the class you created is inheriting a UIViewController. The only reason that xCode won't recognize your class is where it is a different type of instance, let's say, may be a UITableViewController type.

Please check the inheritence of your class first.

Upvotes: 2

Max
Max

Reputation: 809

ok so i dont know how and why, but, i did (AGAIN) what i already tried before which is:

  1. deleting the class
  2. creating the same class with new name
  3. removed the scene and recreated it in the storyboard
  4. connected it all (in the same usual way!!!)

now it seems that Xcode knows my class.... i still don't know what was the problem but i guess it has something to do with the linker.

Upvotes: 1

Related Questions