Fork Me on GitHub

Base iOS View Controllers

Convenient base VCs for all your iPhone projects if you, like me, like writing less code. Allows to very easily create controllers with table views, controllers to show data from core data, adjusting the view with the keyboard easily and automatically, and more.

Download this project as a .zip file Download this project as a tar.gz file

Components

JSBaseViewController
JSBaseTableViewController
JSBaseCoreDataTableViewController
JSBaseTableViewCell

Highlights

Features

- One generic method to construct your controllers:

Forget about implementing the right - initWith, awakeFromNib... just implement

- (void)setUp;

and be sure it will be called once in all cases.

- Easy way to show / hide a flexible progress HUD

Very handy for when you make long-running requests from any view controller.

- (void)showWaitView;
- (void)showWaitViewAndDimScreen;
- (void)showWaitViewWithMessage:(NSString*)message;
- (void)hideWaitView;
- (void)hideWaitViewWithSuccessMessage:(NSString *)message;
- (void)hideWaitViewWithErrorMessage:(NSString *)message;

- Implement table views with very little code:

Forget about dealing with nib loading, cell reusing, etc. Just set the name of the nib file that corresponds to the cell that the table will use in the setUp method like this:

- (void)setUp
{
    self.cellNibName = @"MyCellNibFileName";
}

and than you can just implement this method to make the necessary adjustments to the cell (which you can cast to your own UITableViewCell subclass):

- (UITableViewCell *)tableView:(UITableView *)tv configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;

Only requirement is that your cell inherits from JSBaseTableViewCell.

- Add pull-to-refresh to you tables with one like of code:

Just set

self.tableHasPullToRefresh = YES;

in your setUp method. And then use these two methods:

/* Implement this method to respond to drag to refresh events */
- (void)reloadTableViewDataSource;
/* Call this method when the drag to refresh load has finished */
- (void)doneLoadingTableViewData;

- Implement infinite scrolling in you table views:

Just set:

self.tableHasInfiniteScrolling = YES;

And you will be called to this method:

- (void)loadMoreItemsAfterTheLastOne;

- Automatically have your view adjusted when the keyboard appears / disappears:

Just by calling

[self registerForKeyboardNotifications];

in your setUp method. If it's a regular view controller, you can implement

- (void)viewWillAdjustForKeyboardHidden:(BOOL)keyboardHidden keyboardHeight:(CGFloat)keyboardHeight;

which will be called inside an animation block that goes along with the keyboard. If it's a Table View Controller, the table will automatically adjust. if you want to provide custom behaviour, you can implement

- (void)tableViewWillBeResizedToAdjustForKeyboardHidden:(BOOL)keyboardHidden keyboardHeight:(CGFloat)keyboardHeight;

which will be called inside the animation block.

- Have a view stick on top of the keyboard and animate with it:

(very convenient for typical chat / comment views). Just implement the method:

- (UIView *)keyboardAuxView;

and return the view that you want to stay on top of the keyboard.

- Implement tables that show data from Core Data with very little code:

(and that are automatically refreshed with changes in them). Inherit from JSBaseCoreDataTableViewController and implement:

- (NSFetchRequest *)fetchRequest;

to return an NSFetchRequest object corresponding to the query you want to perform. And then implement this other method that will be called on you:

- (UITableViewCell *)tableView:(UITableView *)tv configureCell:(UITableViewCell *)cell forManagedObject:(NSManagedObject *)object;

- Have all your cells automatically be correctly reused even if you forget to set the reuse identifier in Interface Builder:

For this you only need to make your cell classes inherit from JSBaseTableViewCell.

Submodules

- JSProgressHUD: https://github.com/JaviSoto/JSProgressHUD
- EGOTableViewPullRefresh: https://github.com/enormego/EGOTableViewPullRefresh

Installation

First clone the repository:

$ git clone git@github.com:JaviSoto/Base-iOS-View-Controllers.git
$ cd Base-iOS-View-Controllers
$ git submodule init
$ git submodule update

Second. Drag the BaseControllers folder to your Xcode project. And remove the unneeded files (like the EGOTableViewPullRefresh sample project)

Third. Link against these frameworks:

CoreData
QuartzCore

Fourth. (Optional) Modify the method - (NSManagedObjectContext *)managedObjectContext in JSBaseViewController by adding the needed logic to create a NSManagedObjectContext passing your NSPersistantStoreCordinator (ONLY IF YOU WANT TO USE CORE DATA).

Credits

Thanks to Oriol Blanc, because a lot of this is thanks to him :)