Interface Builder is one of the coolest things about Cocoa development. Being able to draw your interfaces visually can save you tons of otherwise tedious code to create layouts and set visual styles for your user interface elements.
Adrian Kosmaczewski, iPhone and iPad developer and creator of Device DNA and several other apps in the App Store turned us on to nib2objc, which is a brilliant name because it takes NIB (or XIB) files from Interface Builder and converts them to Objective-C.
To run from the command line, simply run
nib2objc, passing the path to your NIB file:
nib2objc yourfile.xib > code.m
The output is a rather nice looking set of UIKit objects and property assignments to match values from your NIB:
// ... 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 *button9 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button9.frame = CGRectMake(167.0, 65.0, 72.0, 37.0); button9.adjustsImageWhenDisabled = YES; button9.adjustsImageWhenHighlighted = YES; button9.alpha = 1.000; button9.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin; button9.clearsContextBeforeDrawing = NO; button9.clipsToBounds = NO; button9.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; // ... [button9 setTitleShadowColor:[UIColor colorWithWhite:0.000 alpha:1.000] forState:UIControlStateSelected]; // ... [view6 addSubview:button9]; // ...
But wait, there’s more. We also stumbled across Adrian’s other project Bluwoki, a Bluetooth peer-to-peer walkie-talkie for iPhone and iPod Touch. Since my daughters get their Dad’s old hand-me-down iPhones, I may have to install this for them to have some fun around the house. Bluwoki is also open source, grab the code on GitHub.
Have comments? Send a tweet to @TheChangelog on Twitter.
Subscribe to The Changelog Weekly – our weekly email covering everything that hits our open source radar.