I think the best way to create forms in iOS is by using FXForms open-source project. It can be found in Github. There are some alternatives, but only FXForms support iOS 6. However, FXForms is still not flexible enough. So, if you want to create something completely custom by design, probably, it will not be a suitable solution.
The main advantage of this approach is that your forms become very compact. Also you will save tons of time and your nerves creating many similar and boring forms after you understand how to use FXForms. But not so easy. The main difficulty is to learn how to declare fields.
There are many examples in the project repository. This is not a tutorial, but rather an introduction. Basically what you do is you inherit from FXFormViewController to create a form view controller.
1 2 3 4 |
#import "RRBaseTableViewController.h" #import "FXForms.h" @interface RRBaseAddResumeViewController : FXFormViewController |
And you create a form object inheriting NSObject, that conforms to FXForm protocol.
Header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#import <Foundation/Foundation.h> #import <FXForms.h> typedef NS_ENUM(NSInteger, Gender) { GenderMale = 0, GenderFemale }; @interface RRCreateResumeForm : NSObject <FXForm> @property (nonatomic, copy) NSString *surname; @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSDate *birthday; @property (nonatomic, assign) Gender gender; @end |
Implementation file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#import "RRCreateResumeForm.h" @implementation RRCreateResumeForm - (NSArray *)fields { return @[ @{FXFormFieldKey: @"surname", FXFormFieldTitle: @"Фамилия"}, @{FXFormFieldKey: @"name", FXFormFieldTitle: @"Имя"}, @{FXFormFieldKey: @"birthday", FXFormFieldTitle: @"Дата рождения"}, @{FXFormFieldKey: @"gender", FXFormFieldOptions: @[@"Мужской", @"Женский"], FXFormFieldTitle: @"Пол"} ]; } @end |
And this is how you stick FormViewController with it’s Form:
1 2 3 4 5 6 7 8 |
- (void)createResumeClicked:(id)sender { RRCreateResumeViewController *createResumeViewController = [[RRCreateResumeViewController alloc] init]; RRCreateResumeForm *form = [[RRCreateResumeForm alloc] init]; createResumeViewController.formController.form = form; [self.navigationController pushViewController:createResumeViewController animated:YES]; } |