使用 UIPickerView/UIDatePicker 的时间选择器
Time picker using UIPickerView/UIDatePicker
我正在努力思考 UIPickerView 以及如何使用它来制作时间选择器。我发现 UIDatePicker 有时间模式,但那是一个组件,而不是视图 class。但我不确定如何制作复杂的 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 inside/instead
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
我决定采用不同的方法,而不是使用 TableView 并将 DatePicker 作为组件。 UITableView
有一个 tableHeaderView
方法,我把选择器放在那里。这样我什至不需要使用自动布局。希望将来能帮助某人。
这是视图本身:
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
我正在努力思考 UIPickerView 以及如何使用它来制作时间选择器。我发现 UIDatePicker 有时间模式,但那是一个组件,而不是视图 class。但我不确定如何制作复杂的 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)
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
我决定采用不同的方法,而不是使用 TableView 并将 DatePicker 作为组件。 UITableView
有一个 tableHeaderView
方法,我把选择器放在那里。这样我什至不需要使用自动布局。希望将来能帮助某人。
这是视图本身:
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