Permalink
Browse files

Add picker_mode to select date, time, date and time or countdown mode…

… for date pickers
  • Loading branch information...
1 parent f224fcb commit 34cdd804ffac80335eabb4e5f634ee06070f9b2f @julienXX julienXX committed Nov 11, 2012
Showing with 27 additions and 4 deletions.
  1. +5 −1 lib/formotion/row/row.rb
  2. +22 −3 lib/formotion/row_type/date_row.rb
View
@@ -13,6 +13,10 @@ class Row < Formotion::Base
# either Formotion::RowType or a string/symbol representation of one
# see row_type.rb
:type,
+ # Stores possible date pickers mode
+ # Available modes [UIDatePickerModeTime, UIDatePickerModeDate, UIDatePickerModeDateAndTime, UIDatePickerModeCountDownTimer]
+ # http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html
+ :picker_mode,
# Stores possible formatting information (used by date pickers, etc)
# if :type == :date, accepts values in [:short, :medium, :long, :full]
:format,
@@ -309,4 +313,4 @@ def load_constants_hack
]
end
end
-end
+end
@@ -26,6 +26,7 @@ def formatter
if date_style && date_style.to_s[-5..-1] != "style"
date_style = (date_style.to_s + "_style").to_sym
end
+
formatter.dateStyle = self.row.send(:const_int_get, "NSDateFormatter", date_style || NSDateFormatterShortStyle)
formatter
end
@@ -44,23 +45,41 @@ def after_build(cell)
def picker
@picker ||= begin
picker = UIDatePicker.alloc.initWithFrame(CGRectZero)
- picker.datePickerMode = UIDatePickerModeDate
+ picker.datePickerMode = self.picker_mode
picker.hidden = false
picker.date = self.date_value || NSDate.date
picker.when(UIControlEventValueChanged) do
- self.row.value = @picker.date.timeIntervalSince1970.to_i
+ self.row.value = format_picker_value(@picker)
update
end
picker
end
end
+ def picker_mode
+ case self.row.picker_mode
+ when :time then UIDatePickerModeTime
+ when :date then UIDatePickerModeDate
+ when :datetime then UIDatePickerModeDateAndTime
+ when :countdown then UIDatePickerModeCountDownTimer
+ else UIDatePickerModeDate
+ end
+ end
+
+ def format_picker_value(picker)
+ case self.row.picker_mode
+ when :time then picker.date.strftime("%I:%M %p")
+ when :countdown then picker.date.strftime("%R")
+ else picker.date.timeIntervalSince1970.to_i
+ end
+ end
+
# Used when row.value changes
def update_text_field(new_value)
self.row.text_field.text = self.formatted_value
end
end
end
-end
+end

0 comments on commit 34cdd80

Please sign in to comment.