48

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.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
Max
  • 799
  • 1
  • 5
  • 17

13 Answers13

97

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.

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
Anthony Scott
  • 1,311
  • 12
  • 9
  • 4
    If ** Inherit from target ** is already selected, deselect it and select it. It worked for me. – Chetan Rajagiri Feb 13 '18 at 06:10
  • 1
    It doesn't work, I have even tried to select and unselect then again select but it still doesn't work and about to getting crazy soon. No solution is found yet. – ACAkgul Oct 17 '18 at 08:47
  • In my case, I had included a class from an Objective-C part of the codebase and included the header in the bridging file. If I ticked Inherit Module from Target, it would crash/fail, if I unticked it, it would be successful. – ColinMasters Jul 05 '19 at 18:04
28

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

dmaulikr
  • 458
  • 5
  • 20
24

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

Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
3

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.

travdu
  • 331
  • 3
  • 9
2

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.

Natasha
  • 6,651
  • 3
  • 36
  • 58
  • Copy the className and paste in the assign class field. If it give any compile error, then there is class incompatibility, otherwise, may be the IE of your xCode isn't working properly. – Natasha Aug 09 '16 at 11:22
2

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.

Ben
  • 1,881
  • 17
  • 20
2

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).

nsinvocation
  • 7,559
  • 3
  • 41
  • 46
  • Dude, you legend! It took me so long to fix this, and I have tried over 30 different answers. Yours worked – MuSoundiX Dec 21 '21 at 12:20
1

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.

Max
  • 799
  • 1
  • 5
  • 17
  • 5
    Almost 2 years later and this is still a problem with Xcode 9.3 Beta 4. What I do is delete the custom class name from IB. Make sure to tab out of the field. Then clean the build folder. Then manually delete all of the caches and derived data folders. Then restart the computer. Then compile the project. Make sure it compiles. Then in IB add the new class name back in. Reset any missing connections. And it just works, to coin a phrase. My case is that I am converting objc classes to swift. And yes everything was hooked up correctly. – Neil Mar 19 '18 at 14:59
1

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.

jhonkaman
  • 535
  • 1
  • 7
  • 14
0

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.

Mo Iisa
  • 466
  • 5
  • 5
0

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
Aaron Zheng
  • 423
  • 3
  • 12
0

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.

sibin
  • 47
  • 9
0

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!

PBE_90
  • 1
  • 3