具有“完成”的UIDatePicker按钮 [英] UIDatePicker with "done" button
问题描述
我需要在这个图像中添加一个完成按钮给我的日期选择器:
I need to add a "done" button to my date picker like in this image:
这是我的代码:
-(IBAction)chooseDate:(id)sender{
self.datepicker= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
self.datepicker.datePickerMode = UIDatePickerModeDate;
datepicker.backgroundColor = Rgb2UIColor(52, 170, 220);
[self.btnDone
addTarget:self
action:@selector(SetDatePickerTime:)
forControlEvents:UIControlEventTouchDown];
[self.view addSubview:self.datepicker];
}
推荐答案
创建日期选择器将其添加为有问题的字段的输入视图( self.fieldInQuestion.inputView = datePicker
)。接下来,创建一个 UIToolBar
(高度44)与 UIBarButton
项目上标题为完成,自我的目标,和一个选择器(例如@selector(完成))。将其添加为相同字段的输入附件视图,您将日期选择器设置为( self.fieldInQuestion.inputAccessoryView = UIToolbarInstance
)的输入视图。在上面的示例中的选择器方法( - (void))中,确保使用[self.fieldInQuestion resignFirstResponder],它将会关闭它。
Create a date picker, then add it as the input view of the field in question (self.fieldInQuestion.inputView = datePicker
). Next, create a UIToolBar
(height 44) with UIBarButton
Item on it with the title "Done", target of self, and a selector (ex. @selector(done)). Add this as the input accessory view of the same field you made the date picker an input view for (self.fieldInQuestion.inputAccessoryView = UIToolbarInstance
). In the selector method (-(void)done in the example above), make sure you use [self.fieldInQuestion resignFirstResponder] and it will dismiss it.
这篇关于具有“完成”的UIDatePicker按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!