Code Monkey home page Code Monkey logo

bzgformviewcontroller's Introduction

BZGFormViewController Build Status

BZGFormViewController is a simple library for making dynamic forms.

alt tag

Demo app

Navigate to SignupForm, run pod install, and open SignupForm.xcworkspace. Build and run to see BZGFormViewController in action.

Installation

Cocoapods is the recommended method of installing BZGFormViewController. Add the following line to your Podfile:

pod `BZGFormViewController`

Quick start

First, subclass BZGFormViewController.

@interface SignupViewController : BZGFormViewController

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

#import "BZGTextFieldCell.h"

// ...

self.usernameCell = [BZGTextFieldCell new];
self.usernameCell.label.text = @"Username";

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

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

Each BZGTextFieldCell contains a BZGInfoCell. The info cell will be displayed if the cell's validationState is either BZGValidationStateInvalid or BZGValidationStateWarning. You can set the info cell's text using setText:.

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

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

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

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

self.formCells = [NSMutableArray arrayWithArray:@[self.usernameCell,
                                                  self.emailCell,
                                                  self.passwordCell]];

Custom sections

BZGFormViewController will only manage one section of your table view. If you want to use a table view with multiple sections, you should specify the section the form view controller should manage.

self.formSection = 0

You'll also need to override the UITableViewDataSource methods. Be sure to use the values from super for 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;
    }
}

Contributing

Please write tests and make sure existing tests pass. Tests can be run from the demo project in /SignupForm. See the Roadmap for planned improvements.

bzgformviewcontroller's People

Contributors

vprtwn avatar

Watchers

 avatar  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.