ios 在文本字段点击上显示日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12828398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:41:09  来源:igfitidea点击:

Show datepicker on textfield tap

iosxcodeuitextfielduidatepicker

提问by lolol

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 textfieldto my problem.

我对 iOS 非常陌生,所以显然我在尝试遵循此解决方案时遗漏了一些概念在点击文本字段时显示日期选择器来解决 我的问题。

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:当工具栏中的完成按钮为单击。也将它们连接起来。方法的实现如下所列。”

I read something about XIB's but I'm using a storyboard, so I think I don't have then. 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.

我读了一些关于 XIB 的东西,但我使用的是故事板,所以我想我没有。另外,我不确定如何在不将元素附加到我的视图控制器的情况下拉出元素(我正在尝试,但是当我释放鼠标按钮时,该工具会飞回工具栏),而我没有甚至不明白为什么我需要 UIToolbar。

And finally, how do I connect my textfield to the datepicker delegate?

最后,如何将我的文本字段连接到日期选择器委托?

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;

回答by Dave DeLong

UIResponder, from which UITextFieldinherits, defines a property called inputView. This property (a UIView *) can define a view that will be displayed instead of the keyboardwhen that UIResponderbecomes the first responder.

UIResponder,从中UITextField继承,定义了一个名为 的属性inputView。这个属性 (a UIView *) 可以定义一个视图,当它成为第一响应者时,它将代替键盘显示UIResponder

Thus, if you want a UIDatePickerto appear when you tap in a textField (ie, you make that textField the first responder), then you can say, na?vely:

因此,如果您希望UIDatePicker在点击 textField 时出现 a(即,您将该 textField 设为第一响应者),那么您可以天真地说:

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

[myTextField setInputView:datePicker];

Now, when your UITextFieldis tapped, a UIDatePickerwill be brought up instead.

现在,当你UITextField被点击时, aUIDatePicker将被调出。

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 inputAccessoryViewproperty is for...)

(如果您想要一个位于 上方的工具栏UIDatePicker,这就是该inputAccessoryView属性的用途...)

回答by iAnkit

In viewDidLoad ofViewController.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];
}

txtFieldBranchYearis an outlet of UITextField

txtFieldBranchYear是 UITextField 的一个出口

回答by artyom

Add a datepicker view and uitoolbarview to your view. For example

将日期选择器视图和 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];

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 textFieldDidBeginEditingsee UITextFieldDelegateyou should fire method that will show your datePicker and toolbar. If you have many textFields to define which is was selected use tagproperty, which is integer and you can use switchinstead of many ifconditions.

在委托textFieldDidBeginEditing的方法中,请参阅UITextFieldDelegate您应该触发将显示您的 datePicker 和工具栏的方法。如果你有很多 textFields 来定义哪个被选中,使用tag属性,它是整数,你可以使用switch而不是许多if条件。

To show your datePicker and toolbar you should change the Y of containerView frame. Here is how to do that

要显示您的 datePicker 和工具栏,您应该更改 containerView 框架的 Y。这是如何做到的

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

To hide just plus the containerViewHeight variable. You can use UIViewAnimations to show it with some animation. For example UIViewAnimationCurveEaseInOut

隐藏仅加上 containerViewHeight 变量。你可以使用 UIViewAnimations 用一些动画来显示它。例如UIViewAnimationCurveEaseInOut