View Controller With No NIB

On the whole, a view controller is created exactly like any other object. A view controller instance comes into existence because you instantiate a view controller class, either in code or by loading a nib / xib / storyboard.  Beginners are taught to make Apps with Storyboard and that is great. Storyboards and Segues are an amazing addition to Interface Builder now all rolled into Xcode.

When working on a client with another developer there were times when we would have merge conflicts due to just even peeking at the Storyboard (which serves as a good reference and documentation for the project). Almost every check-in we had to double check and redo some small change and we were checking in often. There were only two of us so as the team grows these merge conflicts I can only imagine would grow. For the reason of multi-developer environments there are firms who do away with not just Storyboards but Nibs / Xibs in their entirety.  I thought I’d create an App with a TableViewController that has no NIB as a reference for those who have never seen how this is done.  This code is in Github. 

This code is at: https://github.com/duliodenis/TableViewNoIB

I use tags to demonstrate the stages of development I went through.  Once you git clone you get the whole repository and you can do:

git tag

Then start by doing a

git checkout v1.0

This baseline version was created using the Xcode Empty Application Template.  I added to the empty app a New Objective-C Class File for the View Controller which I simply called ViewController and subclassed NSObject in order to do everything manually.

Starting in the AppDelegate implementation I added the import of the new ViewController class and I declared the private myViewController property:

#import "ViewController.h"
@interface AppDelegate()
@property (nonatomic, strong) ViewController* myViewController;
@end

Then in the didFinishLaunchingWithOptions method I added the following two lines to hook the rootViewController to this new ViewController:

self.myViewController = [[ViewController alloc] initWithStyle:UITableViewStyleGrouped];
self.window.rootViewController = self.myViewController;

The only other change I made was in the ViewController.h I had changed the subclass of NSObject to the following:

@interface ViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
@end

This makes myViewController a subclass of UITableViewController that follows the UITableViewDataSource and UITableViewDelegate protocols.

In the next v2.0 tag we’ll explore how to implement the TableView.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s