NIBs or code? Why not both? Here’s nib2objc.

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

[sourcecode lang=’c’]
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.

Posted in Uncategorized

One thought on “NIBs or code? Why not both? Here’s nib2objc.

  1. Leaking Memory!!!I found this piece of code to be incredibly usufel as I have adapted to check to see if there are any pixels remaining in an image I am drawing onto. Could have never figured it out without this. The only problem I have is that the code as it stands leaks memory pretty bad.It probably doesn’t show up much with one touch or button press, but repeated taps (As in my app where I am testing to see if there are any pixels remaining in the contextRef) really show up. You can see the leak pretty well by using the memory monitor in Instruments.I think part of the problem is the deallocation of the bitmapData. I don’t think I am wise enough to figure out where this is supposed to be done. I know that I can slow the leak by using:return [NSData dataWithBytesNoCopy:data length:dataSize freeWhenDone:YES];instead of:return [NSData dataWithBytes:data length:dataSize];The weird thing is when I use NSData dataWithBytesNoCopy I lose access to the buffer in the iPhone Simulator although It works OK in on the actual device. I think that may be an unfortunate bug with dataWithBytes or something.I think there must be a better way of making sure that the original void * bitmapData that is malloc’d is released.Any thoughts?Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *