百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

formotion

> 编程语言
开源

使用 RubyMotion 打造出极其优秀的 iOS 表单

605 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

使用 RubyMotion 打造出极其优秀的 iOS 表单

Formotion

Make this:

using this:

…

And after the user enters some data, do this:

ruby
@form.render
=> {:email=>"[email protected]", :password=>"password", 
    :confirm=>"password", :remember=>true, :account_type=>:pro}

Installation

gem install formotion

In your Rakefile:

require 'formotion'

Usage

Initialize

You can initialize a Formotion::Form using either a hash (as above) or the DSL:

ruby
form = Formotion::Form.new

form.build_section do |section|
  section.title = "Title"

  section.build_row do |row|
    row.title = "Label"
    row.subtitle = "Placeholder"
    row.type = :string
  end
end

Then attach it to a Formotion::FormController and you're ready to rock and roll:

ruby
@controller = Formotion::FormController.alloc.initWithForm(form)
self.navigationController.pushViewController(@controller, animated: true)

Data Types

See the visual list of support row types.

To add your own, check the guide to adding new row types.

Formotion::Form, Formotion::Section, and Formotion::Row all respond to a ::PROPERTIES attribute. These are settable as an attribute (ie section.title = 'title') or in the initialization hash (ie {sections: [{title: 'title', ...}]}). Check the comments in the 3 main files (form.rb, section.rb, and row.rb for details on what these do).

Setting Initial Values

Forms, particularly edit forms, have default initial values. You can supply these in the :value attribute for a given row. So, for example:

ruby
{
  title: "Email",
  key: :email,
  placeholder: "[email protected]",
  type: :email,
  auto_correction: :no,
  auto_capitalization: :none,
  value: '[email protected]'
}

Setting values for non-string types can be tricky, so you need to watch what the particular field expects. In particular, date types require the number of seconds from the beginning of the epoch as a number.

Retrieve

You have form#submit, form#on_submit, and form#render at your disposal. Here's an example:

ruby
class PeopleController < Formotion::FormController
  def viewDidLoad
    super

    self.navigationItem.rightBarButtonItem = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemSave, target:self, action:'submit')
  end

  def submit
    data = self.form.render

    person.name = data[:name]
    person.address = data[:address]
  end
end

Why would you use form#on_submit? In case you want to use type: :submit. Ex:

ruby
@form = Formotion::Form.new({
  sections: [{
  ...
  }, {
    rows: [{
      title: "Save",
      type: :submit
    }]
  }]
})

@form.on_submit do |form|
  # do something with form.render
end

form#submit just triggers form#on_submit.

Persistence

You can easily synchronize a Form's state to disk using the persist_as key in form properties. When your user edits the form, any changes will be immediately saved. For example:

ruby
@form = Formotion::Form.new({
  persist_as: :settings,
  sections: ...
})

This will load the form if it exists, or create it if necessary. If you use remove the persist_as key, then the form will not be loaded and any changes you make will override existing saved forms.

Your form values and state are automatically persisted across application loads, no save buttons needed.

To reset the form, call reset, which restores it to the original state.

View the Persistence Example to see it in action.

Callbacks

Row objects support the following callbacks:

ruby
row.on_tap do |row|
  p "I'm tapped!"
end

row.on_delete do |row|
  p "I'm called before the delete animation"
end

row.on_begin do |row|
  p "I'm called when my text field begins editing"
end

row.on_enter do |row|
  p "I'm called when the user taps the return key while typing in my text field"
end

Forking

Feel free to fork and submit pull requests! And if you end up using Formotion in your app, I'd love to hear about your experience.

License

Todo

  • Not very efficient right now (creates a unique reuse idenitifer for each cell)
  • Styling/overriding the form for custom UITableViewDelegate/Data Source behaviors.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言