你会怎么做?
我有一个带有UITextField的表单。当我点击它时,我想显示一个UIDatePicker,而不是默认弹出的默认键盘?请注意,我的表单上还有其他元素。
答案 0 :(得分:86)
我认为这是一种更正式的方法:
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
datePicker.tag = indexPath.row;
textField.inputView = datePicker;
答案 1 :(得分:9)
in .h
UIDatePicker *datePicker;
NSDate *date;
UIToolbar *keyboardToolbar;
ViewDidLoad中的:
if (keyboardToolbar == nil) {
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
[keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:@"Accept" style:UIBarButtonItemStyleDone target:self action:@selector(closeKeyboard:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, accept, nil]];
}
self.txtDate.inputAccessoryView = keyboardToolbar;
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
self.txtDate.inputView = datePicker;
- (void)datePickerValueChanged:(id)sender{
date = datePicker.date;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/YYYY"];
[self.txtDate setText:[df stringFromDate:date]];
}
故事板中的添加到txtDate - > EditingDidEnd
- (IBAction)establishHour:(id)sender{
hour = hourPicker.date;
NSDateFormatter *hf = [[NSDateFormatter alloc] init];
[hf setDateFormat:@"HH:MM"];
[self.txtHour setText:[hf stringFromDate:hour]];
}
将键盘设置为DatePicker,并创建一个带有按钮的工具栏以接受选择。
答案 2 :(得分:5)
如果有人愿意的话,我已经将Johan Kool的答案转换成了swift。我将代码放在viewDidLoad中。
let datePicker = UIDatePicker()
datePicker.datePickerMode = UIDatePickerMode.Date
datePicker.addTarget(self, action: "datePickerValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
textField.inputView = datePicker
答案 3 :(得分:0)
(IBAction)estableceHora:(id)sender{
Hora = horaPicker.date;
NSDateFormatter *hf = [[NSDateFormatter alloc] init];
[hf setDateFormat:@"HH:MM"];
[self.txtHora setText:[hf stringFromDate:Hora]];
}
答案 4 :(得分:-2)
在.h文件中添加:
UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
在.m文件中添加:
{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
pTxtDate.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:[NSDate date]]];
NSLog(@"pTxtDate.text %@",pTxtDate.text);
[df release];
[self.view addSubview:pTxtDate];
[pTxtDate release];
DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
DatePicker.datePickerMode = UIDatePickerModeDate;
DatePicker.hidden = NO;
DatePicker.date = [NSDate date];
[DatePicker addTarget:self
action:@selector(LabelChange:)
forControlEvents:UIControlEventValueChanged];
[self.view addSubview:DatePicker];
[DatePicker release];
}
- (void)LabelChange:(id)sender {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
pTxtDate.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:DatePicker.date]];
}