简体   繁体   English

在文本字段点击上显示datepicker

[英]Show datepicker on textfield tap

I'm very new to iOS so obviously I'm missing a few concepts when trying to follow this solution Display datepicker on tapping on textfield to my problem. 我对iOS很陌生,所以很明显我在尝试按照这个解决方案时缺少一些概念显示datepicker在文本字段上点击我的问题。

I have the interface and implementation in a class called DatepickerAppDelegate, but I'm lost here: 我在一个名为DatepickerAppDelegate的类中有接口和实现,但我在这里丢失了:

"In the XIB, Pull out a UIToolbar and a UIDatePicker but don't attach it to the view. Connect the outlets appropriately. dateChanged: responds to changes in the date picker and doneEditing: is called when the Done button in the tool bar is clicked. Connect them too. The methods are implemented as listed below." “在XIB中,拉出一个UIToolbar和一个UIDatePicker,但不要将它附加到视图。适当地连接插座.dateChanged:响应日期选择器和doneEditing的变化:当工具栏中的Done按钮是点击。连接它们。方法如下所示。“

I read something about XIB's but I'm using a storyboard, so I think I don't have then. 我读了一些关于XIB的东西,但我正在使用故事板,所以我想我没有。 Also, I'm not sure how I'm supposed to pull out elements without attaching it to my view controllers (I'm trying, but when I release the mouse button, the tool flies back to the toolbar), and I don't even understand why I need a UIToolbar. 此外,我不确定如何在不将其附加到视图控制器的情况下拉出元素(我正在尝试,但是当我释放鼠标按钮时,工具会飞回工具栏),我不知道甚至理解为什么我需要一个UIToolbar。

And finally, how do I connect my textfield to the datepicker delegate? 最后,如何将文本字段连接到datepicker委托?

Can someone help me? 有人能帮我吗?

UPDATE: 更新:

In fact it is very simple, I just need to: 事实上它很简单,我只需要:

datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

self.birthday.inputView = datePicker;

UIResponder , from which UITextField inherits, defines a property called inputView . UITextField继承的UIResponder 定义了一个名为inputView的属性 This property (a UIView * ) can define a view that will be displayed instead of the keyboard when that UIResponder becomes the first responder. UIResponder成为第一个响应者时,此属性( UIView * )可以定义将显示的视图而不是键盘

Thus, if you want a UIDatePicker to appear when you tap in a textField (ie, you make that textField the first responder), then you can say, naïvely: 因此,如果您希望在点击textField时出现UIDatePicker (即,您将textField作为第一个响应者),那么您可以说,天真地:

UIDatePicker *datePicker = [[UIDatePicker alloc] init...];
... configure datePicker ...

[myTextField setInputView:datePicker];

Now, when your UITextField is tapped, a UIDatePicker will be brought up instead. 现在,当您点击UITextField时,将启动UIDatePicker

HOWEVER 然而

You'll want to do more than this, because you'll need to handle different orientations and different idioms. 你需要做的不仅仅是这个,因为你需要处理不同的方向和不同的习语。

But that's the basic idea. 但那是基本的想法。

(And if you want a toolbar above the UIDatePicker , that's what the inputAccessoryView property is for...) (如果你想在UIDatePicker上面有一个工具栏,那就是inputAccessoryView属性的用途......)

In viewDidLoad of ViewController.m 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];
}

Add one method in your ViewController 在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 is an outlet of UITextField txtFieldBranchYear 是UITextField的出口

Add a datepicker view and uitoolbarview to your view. 在视图中添加datepicker视图和uitoolbarview。 For example 例如

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];

So when view did load they are will be hidden Y of contener view is outside of the your view frame. 因此,当视图加载时,它们将被隐藏,竞争视图的Y位于视图框架之外。

Now you should assign property delegate of UITextFieldDelegate 现在您应该分配UITextFieldDelegate的属性委托

textfield.delegate = self;

In the method of delegate textFieldDidBeginEditing see UITextFieldDelegate you should fire method that will show your datePicker and toolbar. 在委托textFieldDidBeginEditing的方法中,请参阅UITextFieldDelegate,您应该触发将显示datePicker和工具栏的方法。 If you have many textFields to define which is was selected use tag property, which is integer and you can use switch instead of many if conditions. 如果你有许多textFields来定义哪个被选中使用tag属性,这是整数,你可以使用switch而不是很多if条件。

To show your datePicker and toolbar you should change the Y of containerView frame. 要显示datePicker和工具栏,您应该更改containerView框架的Y. Here is how to do that 这是怎么做的

CGRect containerViewFrame = containerView.frame;
containerViewFrame.y -=containerViewHeight;
containerView.frame = containerViewFrame;

To hide just plus the containerViewHeight variable. 隐藏加上containerViewHeight变量。 You can use UIViewAnimations to show it with some animation. 您可以使用UIViewAnimations通过一些动画显示它。 For example UIViewAnimationCurveEaseInOut 例如UIViewAnimationCurveEaseInOut

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM