(Somehow this project seems to me so simple, that I’m sure someone has done this before. Anyway). This is my feeble attempt to bring an answer to the eternal dichotomy between those arguing about the relative benefits of creating user interfaces via Interface Builder or via pure Objective-C code: let me introduce nib2objc.
Unbeknown to most of us, the ibtool utility bundled with Interface Builder and Xcode allows us to inspect the contents of NIB files (or XIBs, for that matter) and get from them nice property lists XML streams, which I transform in NSDictionary instances, which I loop over and over util I get something that looks like this:
UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)]; view6.frame = CGRectMake(0.0, 0.0, 320.0, 460.0); view6.alpha = 1.000; view6.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; view6.backgroundColor = [UIColor colorWithWhite:0.750 alpha:1.000]; view6.clearsContextBeforeDrawing = NO; // ... UIButton *view9 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; view9.frame = CGRectMake(167.0, 65.0, 72.0, 37.0); view9.adjustsImageWhenDisabled = YES; view9.adjustsImageWhenHighlighted = YES; view9.alpha = 1.000; view9.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; view9.clearsContextBeforeDrawing = NO; view9.clipsToBounds = NO; view9.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; // ... [view9 setTitleShadowColor:[UIColor colorWithWhite:0.000 alpha:1.000] forState:UIControlStateSelected]; // ... [view6 addSubview:view9]; // ...
Using this tool, I can now use IB for design, and then generate the code for those designs, in case I prefer to use a code-only approach (usually for UITableViewCells, as I explained before). For the moment it only works with UIKit classes, but I don’t think it might be a problem to extend it to AppKit classes as well.
I hope this project is useful to all of you! As usual, open source, public domain and on Github.
Update, 2009-04-09: This project has been featured in an article in Ars Technica by Erica Sadun!
Update, 2010-07-18: Major update to the project! Check out the latest features and the new versions (including a Mac OS X GUI application!)

Comments
15 Comments so far. Leave a comment below.Bravo! I am tired of the back-and-forth between “Use IB” and “Use Xcode”. I appreciate both methods, and this utility bridges the two nicely.
Q: Is there a graceful, easy way to incorporate this into Xcode? For instance, I can imagine wanting to control-click on a NIB/XIB file and selecting “Generate Objective-C” … with the results appearing in a new window or even via one’s preferred text editor.
Glad that you like the project
As for the integration with Xcode, maybe using AppleScript? I admit I haven’t tried, it’s a good idea though.
A followup to my previous comment: I decided to “Create a User Script” via the Xcode script menu (next to Help) … except I haven’t found a way to obtain the currently selected file (vs. the contents of the currently open file). NIB/XIB files don’t open within Xcode, so here’s the next best thing. Create a User Script with Input and Directory both set to “Selection”, and Output/Errors set to taste. I called my script “Convert NIB/XIB to Objective-C”. (My apologies in advance if this isn’t formatted well in the comments!)
! /bin/sh
#
Converts a NIB or XIB to Objective-C
FILE=
%%%{PBXUtilityScriptsPath}%%%/AskUserForExistingFileDialog "Choose a NIB or XIB to convert to Objective-C"cat <<EOT /* * nib2objc – NIB/XIB to Objective-C Converter * http://kosmaczewski.net/2009/03/17/nib2objc/ * Source: {$FILE} */ EOT /path/to/nib2objc “$FILE”Hey thanks! That’s great. I’ll try that asap.
You bet! Meanwhile, I have two subtle edits: Remove the curly braces around $FILE, and add an extra/blank line before the EOT on a line by itself.
Chalk it up to cosmetics.
Hey! Just found out about your nib2objc. I was wondering if you know of any documentation online about the nib file format. I understand that its basically an XML file but couldn’t find any spec for it.
Hi Cesar, thanks for your comment! Actually no, I just inspected the XIB file by hand, and it has a fairly simple structure, not really hard to understand. And ibtool exposes most of the data for you, at least what I needed for this project.
Seems that IB 3.2.2 breaks nib2obj. I’m getting only:
[view90 addSubview:view93]; [view90 addSubview:view98]; [view90 addSubview:view99];etc. Great app, though!Thanks for the comments Alan! I’m working on the next version, with the integration of the next [REDACTED] SDK
stay tuned.
oops… ignore last comment – just noticed UIKit only – not AppKit
Adrian, I really appreciate this! It is exactly what I was looking for!
Sorry to bother, but I was hoping for some help in figuring out how I could implement your utility in an iPhone/iPad application. I attempted placing the pure source code into my application, but ran into problems with the “Cocoa/Cocoa.h” headers and NSSize, NSPoint, NSTask, presumably because they are not supported by the iPhone development.
I’m going through it all and will hopefully be able to replace the Cocoas with UIKits, the NSSize with CGSize, etc., but if there is some sort of method in which I can include the final, compiled utility, and call it from the application?
Thanks in advance!
Yup, I fixed everything but cannot get past NSTask. The iPhone has no substitutable equivalent. I therefore assume that the compiled utility will also not work on the iPhone.
I’d still appreciate any assistance in finding some sort of way to create this functionality on the iPhone platform!
-Dan
NSTask is used solely for the purpose of running the executable ibtool from the iphone development system and collecting the ouput from the exectuable. If you don’t have ibtool on the iphone, it would be pointless to try find an NSTask substitute for the iphone. If ibtool were available, one workaround would be capture the output of ibtool in a file or pipe and have your converted program read in the data.
-Isaac
Is there a way to get nib and use it in ur Xcode project like converting it somehow to get the code or xib? And I made a couple of games and I am willing to trade a game for an app u made email me at deadbully@yahoo.com
I’ve just updated the project with new features and a Mac OS X application, as well as a Mac OS X service! Check out the new changes here and here. Enjoy!