GithubHelp home page GithubHelp logo

peterzakin / bzgformviewcontroller Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vprtwn/bzgformviewcontroller

0.0 2.0 0.0 3.49 MB

A library for making dynamic forms. (iOS)

License: MIT License

bzgformviewcontroller's Introduction

BZGFormViewController

BZGFormViewController is a simple library for making dynamic forms.

alt tag

Demo app

Navigate to /SignupForm, run pod install, and open SignupForm.xcworkspace to see the signup form above in action.

Installation

Cocoapods, or copy the contents of /BZGFormViewController into your project.

Quick start

First, subclass BZGFormViewController.

@interface SignupViewController : BZGFormViewController

Next, import "BZGFormFieldCell.h" and create a cell.

#import "BZGFormFieldCell.h"

// ...

self.usernameFieldCell = [BZGFormFieldCell new];
self.usernameFieldCell.label.text = @"Username";
self.usernameFieldCell.textField.placeholder = @"Username";
self.usernameFieldCell.textField.keyboardType = UIKeyboardTypeASCIICapable;

To validate text and update the cell whenever the field's text changes, use the cell's shouldChangeTextBlock.

self.usernameFieldCell.textField.delegate = self;
self.usernameFieldCell.shouldChangeTextBlock = ^BOOL(BZGFormFieldCell *cell, NSString *newText) {
    if (newText.length < 5) {
        cell.validationState = BZGValidationStateInvalid;
    } else {
        cell.validationState = BZGValidationStateValid;
    }
    return YES;
};

Each BZGFormFieldCell has a BZGFormInfoCell property. If validation fails, you should set the info cell's text using setText:. To show the info cell, set shouldShowInfoCell to YES. Don't forget to hide the info cell when you have nothing to show.

self.usernameFieldCell.shouldChangeTextBlock = ^BOOL(BZGFormFieldCell *cell, NSString *newText) {
    if (newText.length < 5) {
        cell.validationState = BZGValidationStateInvalid;
        [cell.infoCell setText:@"Username must be at least 5 characters long."];
        cell.shouldShowInfoCell = YES;
    } else {
        cell.validationState = BZGValidationStateValid;
        cell.shouldShowInfoCell = NO;
    }
    return YES;
};

You should use shouldChangeTextBlock, didBeginEditingBlock, didEndEditingBlock, and shouldReturnBlock for validation and any logic you would usually put in UITextFieldDelegate methods.

BZGFormViewController automatically scrolls the tableview when you begin editing a field and jumps to the next field when you hit return.

After you've configured your cells, set formFieldCells to an array containing your form's cells.

self.formFieldCells = [NSMutableArray arrayWithArray:@[self.usernameFieldCell,
                                                       self.emailFieldCell,
                                                       self.passwordFieldCell]];

If you're using a table view with multiple sections, specify the section your form (i.e. the cells in self.formFieldCells) should appear in.

self.formSection = 0

Finally, override the UITableViewDataSource methods. Be sure to use the values from super when you're dealing with the table view's form section.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == self.formSection) {
        return [super tableView:tableView numberOfRowsInSection:section];
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == self.formSection) {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    } else {
        return self.signupCell;
    }
}

Configuration

If you want to customize things like fonts and colors, first try forking and editing /BZGFormViewController/Constants.h. If there's anything you can't configure from there, please submit an issue (or pull request)!

Contributing

Please write tests and make sure existing tests still pass. I'll add Travis soon, but for now, run tests from the demo project in /SignupForm. See the Roadmap for planned improvements.

bzgformviewcontroller's People

Contributors

vprtwn avatar

Watchers

Peter Zakin avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.