4

I have implemented one UIButton-derived class (RoundButton) in Swift and the rest is still in Objective-C. How do I access both Objective-C-based classes and Swift-based classes in the same storyboard?

You can type RoundButton in a UIButton Custom Class - Class field in a Storyboard, but iOS gives the following warning runtime:

Unknown class RoundButton in Interface Builder file.
Markus Rautopuro
  • 7,997
  • 6
  • 47
  • 60

4 Answers4

4

It seems that Xcode 6.0 (6A215l) needs to upgrade the Storyboard file to a newer version before it can access Swift classes.

If you replaced an Objective-C class with a Swift class with the same name, you need to open the Storyboard, edit the name of the custom class (like remove the last letter), save the Storyboard and then rename the class back. This will force the Storyboard file to be upgraded to a newer version, and the new Xcode to correctly write the custom class key like this:

Version information diff: enter image description here

UIButton custom class definition diff: enter image description here

After these changes in the Storyboard file the warning seems to disappear and the correct custom class is used.

Markus Rautopuro
  • 7,997
  • 6
  • 47
  • 60
  • I wanted to add that I was having same problems. And renaming the class also fixed the issue. In my case I rewrote a working Objective-C class in Swift. This class was called BTZGenres.m. The new class I created was called Genres.swift (note I removed "BTZ"). In Storyboard scene I changed the UIViewController class to use the new swift based class. I also point out the I was using a newer version of Xcode (6.1.1). If this is Xcode bug then it's not fixed as of 6.1.1. This project was originally built in Xcode 5. – xdeleon Feb 22 '15 at 20:32
  • It seems to be a bug in Xcode. Renaming is critical for storyboard to reload the class. Note that, Swift class in storyboard has extra module information. In your case, you defined a module named 'fantter' in your project's target. – DawnSong Mar 13 '16 at 02:07
1

I wanted to add two other solutions that worked for me, in two separate cases were I got the same message (Unknown class MySwiftClass in Interface Builder file.)

In the first case, I had a Swift subclass of a UIViewController. Well I had thought I wanted to set something in init so I had implemented:

override func initWithNibName:bundle:

As that's the designated initializer... I was every doing anything in the method yet, it was just in place. And yet at runtime my class was not seen until I removed that method.

The other case I had a different Swift UIViewController subclass that also was not being found at runtime. The end fix for that was to re-name the Swift class (I added a "2" into the name). I changed the class also in the storyboard, and suddenly it worked... this was after trying to remove the name from the storyboard and re-adding, which did not work.

Both of these cases may really be other examples of the accepted answer - but in my case just changing the name in the storyboard was not enough, I had to change the swift class itself in some way.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
0

You should make sure that you have added Objective C bridging header. It is not a problem to use both Objective C and Swift in the same storyboard.

http://i58.tinypic.com/33aqbyu.png

Todor Brachkov
  • 219
  • 1
  • 9
0

It indeed seems like an issue with the Storyboard file itself in Xcode 6.0 (6A254o) / iOS 8 Beta 3.

If you're using pre-Xcode 6 Storyboard version it might help to manually update the subclass that Interface Builder is complaining about simply by forcing update of the 'Custom Class' field in the 'Identity Inspector'.