IOS and Rails web dev at Intotheweb. Follow me on twitter @brunow
Create iPhone and iPad (IOS) FORM with BaseKit
If you have ever developed with Rails you probably now form_for for people thaht don"t now here is an example of a form linked with a model:
<%= form_for @article |f| %>
<%= f.text_field :title %>
<%= f.text_area :body %>
<%= f.submit "Create" %>
<% end %>
BaseKitFormMapping let you create simple form linked with a model easily.
Here's a screenshot of the form that we're going to create:

Installation
First step install BaseKit. To do that the easiest way is by using Cocoapods. You just need to add this line to your podfile
dependency 'BaseKit/FormMapping', '~> 0.2.3'
After that then run
pod install
If you don't want Cocoapods just copy Code/Core, Code/UIView, Code/FormField, Code/FormMapping, Vendor/ActionSheetPicker, and Vendor/BWLongTextViewController. Last bu not least ActionSheetPicker is not ARC friendly, so you need to add -fno-objc-arc flag on each file.
Model creation
To store form informations we need to create a model. Create a new Objective-C class called Movie.
Movie.h
@interface Movie : NSObject
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSDate *createdAt;
@property (nonatomic, retain) NSNumber *suitAllAges;
@end
Movie.m
@implementation Movie
@synthesize title, createdAt, suitAllAges;
@end
FormTableViewController
Add a new Objective-C class called FormTableViewController
FormTableViewController.h
@class BKFormModel;
@class Movie;
@interface FormTableViewController : UITableViewController
@property (nonatomic, strong) BKFormModel *formModel;
@property (nonatomic, strong) Movie *movie;
@end
FormTableViewController.h
@implementation FormTableViewController
@synthesize formModel, movie;
- (id)init {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.formModel = [BKFormModel formTableModelForTableView:self.tableView];
self.movie = [[Movie alloc] init];
self.movie.title = @"Movie title";
self.movie.suitAllAges = [NSNumber numberWithBool:NO];
[BKFormMapping mappingForClass:[Movie class] block:^(BKFormMapping *formMapping) {
[formMapping sectiontTitle:@"Information section" identifier:@"info"];
[formMapping mapAttribute:@"title" title:@"Title" type:BKFormAttributeMappingTypeText];
[formMapping mapAttribute:@"createdAt" title:@"ReleaseDate" type:BKFormAttributeMappingTypeDatePicker dateFormat:@"yyyy-MM-dd HH:mm:ss"]];
[formMapping mapAttribute:@"suitAllAges" title:@"All ages" type:BKFormAttributeMappingTypeBoolean];
[self.formModel registerMapping:formMapping];
}];
[self.formModel loadFieldsWithObject:self.movie];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.formModel = nil;
self.movie = nil;
}
Small explanation
self.formModel = [BKFormModel formTableModelForTableView:self.tableView];
Register the table view
[BKFormMapping mappingForClass:[Movie class] block:^(BKFormMapping *formMapping)
Create a form mapping for Movie class**
[formMapping sectiontTitle:@"Information section" identifier:@"info"];
Create a section with title: "Information section", identifier doesn't matter
[formMapping mapAttribute:@"title" title:@"Title" type:BKFormAttributeMappingTypeText];
Here we map movie.title property and say that cell must be of text type
[formMapping mapAttribute:@"createdAt" title:@"ReleaseDate" type:BKFormAttributeMappingTypeDatePicker dateFormat:@"yyyy-MM-dd HH:mm:ss"]];
Show a data picker on touch. DateFormat is used on date displaying
[self.formModel loadFieldsWithObject:self.movie];
This step load the form