Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode 4, Interface Builder and the Awareness of Classes in a Static Library

Although Xcode 4 is advertised being aware of cross-project classes of projects in a shared workspace, I didn't find this working and did some research about the problem. The common solution discussed in many threads around here is, to create a static library and link your projects against it.

I therefore followed Jonah's guide (http://blog.carbonfive.com/2011/04/04/using-open-source-static-libraries-in-xcode-4/) and created a static library to share frequently used subclasses of UIView with multiple projects.

With some trial and error experience and troubleshooting, I found this solution really satisfying and clean, but there are still some complications:

(1) A static library is unable to share XIB files

After the research I did, it seems impossible to access XIB files from outside the static library project without including them by dragging them into every project they are needed to create references. Given a UIViewController subclass in the static library which should be loaded from a XIB - although the UIViewController subclass is accessible from any project that links against the static library, you can't call 'initWithNibName' without adding the XIB file to that project.

I temporally solved this issue just by creating references to the XIB files of the static library in every project I need them - is there a better solution?

(2) Interface Builder isn't aware of classes in a static library

Although I can select one of the subclasses of UIView in my static library as the class of an object I dragged into an XIB in my project (even with autocompletion), I get an error at runtime saying "Unknown class [UIViewSubclassName] in Interface Builder file".

The solution I found here - Interface Builder can't see classes in a static library - (dragging the header file into the XIB-Browser to make Interface Builder recognize the class) apparently stopped working with the new Xcode 4 - the XIB-Browser is merged into the single-window-interface and doesn't respond to header files being dragged into it.

How can I make my XIB include objects from a static library (e.g. a UIViewController subclass in my project that should contain some buttons I frequently use and therefore included in my static library) without throwing exceptions at runtime?

Thank you very much!

PS: Some of these errors occur with code that compiles for the simulator without issues, but throw errors when building for the device...

like image 336
knl Avatar asked Apr 18 '11 18:04

knl


1 Answers

I had a similar issue and solved it by declaring a category on the class I wished included in the project. This forced the class to be loaded from the static library. There wasn't any actual methods defined in the class.

There are some other alternatives that are commonly bandied about like setting linker flags -ObjC and -all_load

** update in response to request for code.

I added a Group to the project named "Categories to force class loads" and, for example, to force the "SimpleAppStateManager" class to be included in this project (that was defined in the shared static library) added these 'empty' category files:

SimpleAppStateManager+DART.h

#import <Foundation/Foundation.h>
#import "SimpleAppStateManager.h"

@interface SimpleAppStateManager (DART)
@end

SimpleAppStateManager+DART.m

#import "SimpleAppStateManager+DART.h"

@implementation SimpleAppStateManager (DART)
@end
like image 136
software evolved Avatar answered Nov 15 '22 15:11

software evolved