Grouped スタイルの UITableView を使ったフォームを簡単に作成できる、 RubyMotion 用の gem 『Formotion』が便利。
文字列入力や数値入力やスイッチなど、たいていの入力は README 読めば使える。 ただ自分の場合、日付選択とサブフォームで嵌った。 特にサブフォームは使い方が難解で、ソースコードを読まないと使いこなすのは難しいと思う。
Formotion のソースコードと睨めっこしながら書いた、 日付選択とサブフォームのサンプルが下記になる。
# coding: utf-8 class MyFormController < Formotion::FormController def init form = Formotion::Form.new({ sections: [{ title: "Register", rows: [{ title: "Date", type: :date, key: :deadline, # 現在時刻を整数値に変換して渡す value: Time.now.to_i, }] },{ title: "Subform", rows: [{ title: "Account type", type: :subform, # render したとき subform 行の値は # { :account => { :account_type => :Basic } } # になる key: :account, # subform を render して取り出した Hash の # キー :account_type に対する値を表示する display_key: :account_type, subform: { sections: [{ # このセクションで選択した行の key が # キー :account_type に対する値として格納される # (例) { :account_type => :Basic } key: :account_type, select_one: true, rows: [{ title: "Free", key: :Free, type: :check, }, { title: "Basic", key: :Basic, type: :check, value: true, }, { title: "Pro", key: :Pro, type: :check, }] }] } }] }] }) initWithForm(form) end def viewDidLoad super self.title = "FormotionSample" self.navigationItem.rightBarButtonItem = UIBarButtonItem.alloc.initWithBarButtonSystemItem( UIBarButtonSystemItemDone, target:self, action:"show_data" ) end def show_data data = self.form.render App.alert(data.to_s) end end class AppDelegate def application(application, didFinishLaunchingWithOptions:launchOptions) @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) @window.rootViewController = UINavigationController.alloc.initWithRootViewController( MyFormController.alloc.init ) @window.makeKeyAndVisible true end end
自分用の備忘録なのでコメント多め。 Formotion でサブフォーム使うたびに読み返すことになるだろうな。