Skip to content

alinradut/nib2objc

 
 

Repository files navigation

nib2objc – NIB to Objective-C converter

This utility converts NIB files (or XIB ones) into Objective-C code, including all the properties of each instance, the documented constructor calls, and also the view hierarchy. It uses the output of the ibtool utility bundled with the Xcode tools. For more information about ibtool, please check the ibtool man page.

Usage

Compile and put the utility in your path, and then call

nib2objc yourfile.xib > code.m

This will generate a file with the output of the conversion, similar to this (check out the included sample.m file for the full version):

    // ...
    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];
    // ...

The generated code takes into account the real class hierarchies in the UIKit, and it outputs the correct constructors and/or method calls for each object, depending on its class, including the enum values corresponding to the properties using them. It also adds name aliases for items that have been named, so that dependant code does not need to be adjusted for each update that changes the order of components.

Mac OS X GUI Application

The “GUI” folder contains a Mac OS X application that can be used to open several nib files and to see their contents as Objective-C code.

The GUI application uses the Fragaria library as a submodule, to provide syntax highlighting of the generated Objective-C code. Prior to compilation, please do the following:

$ git submodule init
$ git submodule update

Mac OS X Service

A secondary project in the “Convert with nib2objc” folder creates an Automator action, that can be used to create Mac OS X services with Automator. The “Convert with nib2objc.zip” file contains an example of such a service, ready to be installed in ~/Library/Services, and which can be called from the Finder after selecting a nib file. The result of the conversion will be placed in the clipboard for pasting into any application.

How to make use of generated code in your project

Copy the compiled nib2objc binary to /usr/local/bin or modify the nib2objc.py script to point to it.

Add a new ‘Shell Script Target’ to the project, set the script as /usr/local/bin/nib2objc.py (need to copy script first) or [nib2objc project path]/Contrib/nib2objc.py
(this uses the environment variable to indentify the project directory and creates a XibFileName.xib.m file for each XibFileName.xib found)

Set the script target as a dependency of the desired build target so it gets run before the build.

Override the initWithNibName and include the relevant xib.m file, then attach any controls you need to retain or add actions to.

For a View:


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
#include “XibFileName.xib.m”
self.view=MainView;
self.label=Label;
[IncrementButton addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}

For a TableViewController:


#include “TableView.xib.m”
self.tableView = TableView;
[tableView setDelegate:self];
[tableView setDataSource:self];
[self.view addSubview:MainView];

For a TableViewCell (MainView is a subview of the cell that contains all the elements):


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
#include “XibFileName.xib.m”
[self addSubview:MainView];
}
return self;
}

Configure your source control to ignore *.xib.m files as these are auto created during the build process and don’t need tracking.

Limitations

  • For the moment this utility only works for UIKit views (iPhone), but it should be easy to extend to those of the AppKit too.
  • It cannot output the values of UIImage, NSLocale or NSTimeZone properties, because ibtool does not support them.
  • It does not distinguishes default values for properties, and as such, it outputs all the information it can find about a particular instance.

Contributions

This project includes the nibs2objc.pl Perl script by Rudi Farkas, which allows users to perform batch transformations of nib files in a folder. Thanks Rudi for the contribution!

About

Set of tools and utilities (command line, GUI, Mac Service) to transform NIB files for the iPhone into Objective-C code files

Resources

License

Stars

Watchers

Forks

Packages

No packages published