在文本字段点击上显示datepicker [英] Show datepicker on textfield tap

查看:120
本文介绍了在文本字段点击上显示datepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我在名为DatepickerAppDelegate的类中有接口和实现,但我是丢失在这里:

I have the interface and implementation in a class called DatepickerAppDelegate, but I'm lost here:

在XIB中,拔出UIToolbar和UIDatePicker但不要将其附加到视图。适当地连接插座.dateChanged:响应日期选择器和doneEditing的变化:单击工具栏中的完成按钮时调用。连接它们。方法如下所示。

我读过有关XIB的内容,但我正在使用故事板,所以我想我没有。此外,我不确定如何在不将其附加到我的视图控制器的情况下拉出元素(我正在尝试,但是当我释放鼠标按钮时,工具会飞回工具栏),我不知道甚至理解为什么我需要一个UIToolbar。

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.

最后,如何将我的文本字段连接到datepicker委托?

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

有人能帮帮我吗?

更新:

事实上它很简单,我只是需要to:

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 UITextField 继承,定义一个名为 inputView 的属性。此属性( UIView * )可以定义一个视图,当该 UIResponder 时,将显示而不是键盘 code>成为第一个响应者。

UIResponder, from which UITextField inherits, defines a property called inputView. This property (a UIView *) can define a view that will be displayed instead of the keyboard when that UIResponder becomes the first responder.

因此,如果您想点击 UIDatePicker 在textField中(即,你将textField作为第一个响应者),然后你可以说,天真地:

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:

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

[myTextField setInputView:datePicker];

现在,点击你的 UITextField 时,一个 UIDatePicker 将被提起。

Now, when your UITextField is tapped, a UIDatePicker will be brought up instead.

你会想做更多的事情,因为你需要处理不同的方向和不同的习语。

You'll want to do more than this, because you'll need to handle different orientations and different idioms.

但这是基本的想法。

(如果你想要一个工具栏高于 UIDatePicker ,那就是 inputAccessoryView 属性适用于......)

(And if you want a toolbar above the UIDatePicker, that's what the inputAccessoryView property is for...)

这篇关于在文本字段点击上显示datepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆