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