使用UIPickerView / UIDatePicker的时间选择器

时间:2016-08-22 18:09:51

标签: rubymotion

我试图绕过UIPickerView以及如何使用它来制作时间选择器。我发现UIDatePicker有时间模式,但它是一个组件,而不是视图类。但我不知道如何制作复杂的PickerView而不是一些字符串。这就是我到目前为止所做的:

class NewAlarmViewController < UIViewController
  def viewDidLoad
    super

    self.title = "Add Alarm"
  end

  def loadView
    self.view = NewAlarmView.new

    self.view.showsSelectionIndicator = true
    self.view.dataSource = self
    self.view.delegate = self
  end

  # Necessary to implement these methods in order to be a datasource
  # - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
  def numberOfComponentsInPickerView pickerView
    2
  end

  # - (NSInteger)pickerView:(UIPickerView *)pickerView
  # numberOfRowsInComponent:(NSInteger)component;
  def pickerView(pickerView, numberOfRowsInComponent:component)
    24
  end

  def pickerView(pickerView, titleForRow:row, forComponent:component)
    "#{row}"
  end

  private

    def create_new_alarm(attributes)
      Alarm.create(attributes)
      Alarm.save
    end
end

我的观点在这里。我将DatePicker注释掉,因为它只是位于选择器的顶部,这显然不是我想要的。我不知道如何在/ {而不是pickerView(pickerView, titleForRow:row, forComponent:component)

中使用DatePicker
class NewAlarmView < UIPickerView
  # attr_accessor :button

  def init
    super

    self.backgroundColor = UIColor.whiteColor

    # date_picker = UIDatePicker.new
    # date_picker.frame = [[0, 150], [200, 500]]
    # date_picker.datePickerMode = UIDatePickerModeTime
    # addSubview(date_picker)

    self
  end
end

1 个答案:

答案 0 :(得分:0)

我决定以不同的方式处理它,而是使用TableView和DatePicker作为组件。 tableHeaderView有一个UITableView方法,我把选择器放在那里。这样我甚至不需要使用Autolayout。希望将来帮助某人。

以下是视图本身:

class NewAlarmView < UITableView
  attr_accessor :date_picker

  def initWithFrame(frame, style:style)
    super

    self.backgroundColor = UIColor.whiteColor
    self.scrollEnabled = false

    @date_picker                = UIDatePicker.new
    @date_picker.datePickerMode = UIDatePickerModeTime
    self.tableHeaderView        = @date_picker

    self
  end
end