[英]Show datepicker on textfield tap
我对iOS很陌生,所以很明显我在尝试按照这个解决方案时缺少一些概念显示datepicker在文本字段上点击我的问题。
我在一个名为DatepickerAppDelegate的类中有接口和实现,但我在这里丢失了:
“在XIB中,拉出一个UIToolbar和一个UIDatePicker,但不要将它附加到视图。适当地连接插座.dateChanged:响应日期选择器和doneEditing的变化:当工具栏中的Done按钮是点击。连接它们。方法如下所示。“
我读了一些关于XIB的东西,但我正在使用故事板,所以我想我没有。 此外,我不确定如何在不将其附加到视图控制器的情况下拉出元素(我正在尝试,但是当我释放鼠标按钮时,工具会飞回工具栏),我不知道甚至理解为什么我需要一个UIToolbar。
最后,如何将文本字段连接到datepicker委托?
有人能帮我吗?
更新:
事实上它很简单,我只需要:
datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
self.birthday.inputView = datePicker;
UITextField
继承的UIResponder
定义了一个名为inputView
的属性 。 当UIResponder
成为第一个响应者时,此属性( UIView *
)可以定义将显示的视图而不是键盘 。
因此,如果您希望在点击textField时出现UIDatePicker
(即,您将textField作为第一个响应者),那么您可以说,天真地:
UIDatePicker *datePicker = [[UIDatePicker alloc] init...];
... configure datePicker ...
[myTextField setInputView:datePicker];
现在,当您点击UITextField
时,将启动UIDatePicker
。
你需要做的不仅仅是这个,因为你需要处理不同的方向和不同的习语。
但那是基本的想法。
(如果你想在UIDatePicker
上面有一个工具栏,那就是inputAccessoryView
属性的用途......)
在 ViewController.m的 viewDidLoad中
- (void)viewDidLoad {
[super viewDidLoad];
UIDatePicker *datePicker = [[UIDatePicker alloc]init];
[datePicker setDate:[NSDate date]];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
[txtFieldBranchYear setInputView:datePicker];
}
在ViewController中添加一个方法
-(void) dateTextField:(id)sender
{
UIDatePicker *picker = (UIDatePicker*)txtFieldBranchYear.inputView;
[picker setMaximumDate:[NSDate date]];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *eventDate = picker.date;
[dateFormat setDateFormat:@"dd/MM/yyyy"];
NSString *dateString = [dateFormat stringFromDate:eventDate];
txtFieldBranchYear.text = [NSString stringWithFormat:@"%@",dateString];
}
txtFieldBranchYear 是UITextField的出口
在视图中添加datepicker视图和uitoolbarview。 例如
CGFloat toolbarHeight = 44.f;
CGFloat datePickerHeight = 140.f;
CGFloat containerViewHeight = toolbarHeight+datePickerHeight;
//create container view for datepicker and toolbar
UIView *containerView = [[UIVIew alloc] initWithFrame:CGRectMake(0.f, [self.view bounds].size.height+containerViewHeight, [self.view bounds].size.width, containerViewHeight)];
//after init toolbar set the frame
toolBar.frame = CGRectMake(0.f,0.f,containerView.frame.size.width, toolbarHeight];
//then add on toolbar button save and release it after add
//after init datePicker set the frame
datePicker.frame = CGRectMake(0.f, toolbarHeight, containerView.frame.size.width, datePickerHeight);
//add datePicker and toolBar to containerView
[containerView addSubview: toolBar];
[containerView addSubview: datePicker];
[toolBar release];
[datePicker release];
//add containerView to main view
[self.view addSubview: containerView];
[containerView release];
因此,当视图加载时,它们将被隐藏,竞争视图的Y位于视图框架之外。
现在您应该分配UITextFieldDelegate的属性委托
textfield.delegate = self;
在委托textFieldDidBeginEditing的方法中,请参阅UITextFieldDelegate,您应该触发将显示datePicker和工具栏的方法。 如果你有许多textFields来定义哪个被选中使用tag属性,这是整数,你可以使用switch而不是很多if条件。
要显示datePicker和工具栏,您应该更改containerView框架的Y. 这是怎么做的
CGRect containerViewFrame = containerView.frame;
containerViewFrame.y -=containerViewHeight;
containerView.frame = containerViewFrame;
隐藏加上containerViewHeight变量。 您可以使用UIViewAnimations通过一些动画显示它。 例如UIViewAnimationCurveEaseInOut
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.