(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!
Comments
10 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