Display datepicker on tapping on textfield(在点击文本字段时显示日期选择器)
问题描述
如何在点击文本框时显示 datetimepicker 控件?
How can I display a datetimepicker control on tap of a textbox?
我有一个包含到达和离开文本字段的用户界面,当用户点击到达文本框时,它应该会弹出一个 datetimepicker 控件而不是键盘,并且与离开文本框相同.
I have a user interface that has arrival and departure text fields, and when a user clicks on arrival textbox it should bring up a datetimepicker control up instead of a keyboard and the same with the departure textbox.
推荐答案
您可以为此使用文本字段的 inputView
和 inputAccessoryView
属性.创建日期选择器并将其设置为两个文本字段的输入视图.还为 Done
按钮创建另一个视图,并将其作为辅助视图.您将需要该按钮来关闭输入视图.
You can use inputView
and inputAccessoryView
properties of the text field for this. Create the date picker and set it to the input views of the two text fields. Also create another view for the Done
button and it as their accessory view. You will need that button to dismiss the input view.
Done
按钮必须连接到基本上执行此操作的功能 -
The Done
button must be wired up to function which basically does this –
if ( [textField1 isFirstResponder] ) {
[textField1 resignFirstResponder];
} else if ( [textField2 isFirstResponder] ) {
[textField2 resignFirstResponder];
}
另一个选择是继承 UITextField
并覆盖 inputView
和 inputAccessoryView
.当有大量它们时,这是要走的路.
Another option would be to subclass UITextField
and override inputView
and inputAccessoryView
. This is the way to go when there are loads of them.
示例
@interface CustomKeyboardAppDelegate : NSObject <UIApplicationDelegate> {
...
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) IBOutlet UIToolbar *accessoryView;
@property (nonatomic, retain) IBOutlet UIDatePicker *customInput;
- (IBAction)dateChanged:(id)sender;
- (IBAction)doneEditing:(id)sender;
@end
在 XIB 中,拉出一个 UIToolbar
和一个 UIDatePicker
但不要将其附加到视图.正确连接插座.dateChanged:
响应日期选择器中的更改,并在单击工具栏中的 Done
按钮时调用 doneEditing:
.也连接它们.这些方法的实现如下所列.
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.
@implementation CustomKeyboardAppDelegate
@synthesize window=_window;
@synthesize textField = _textField;
@synthesize accessoryView = _accessoryView;
@synthesize customInput = _customInput;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.textField.inputView = self.customInput;
self.textField.inputAccessoryView = self.accessoryView;
...
}
...
- (IBAction)dateChanged:(id)sender {
UIDatePicker *picker = (UIDatePicker *)sender;
self.textField.text = [NSString stringWithFormat:@"%@", picker.date];
}
- (IBAction)doneEditing:(id)sender {
[self.textField resignFirstResponder];
}
@end
随着更多的文本字段依赖于这个选择器,最后两种方法会膨胀.
The last two methods will bloat up as more text fields depend on this picker.
这篇关于在点击文本字段时显示日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在点击文本字段时显示日期选择器


基础教程推荐
- 固定小数的Android Money Input 2022-01-01
- “让"到底是怎么回事?关键字在 Swift 中的作用? 2022-01-01
- Android ViewPager:在 ViewPager 中更新屏幕外但缓存的片段 2022-01-01
- Android文本颜色不会改变颜色 2022-01-01
- :hover 状态不会在 iOS 上结束 2022-01-01
- 在 iOS 上默认是 char 签名还是 unsigned? 2022-01-01
- 使用 Ryzen 处理器同时运行 WSL2 和 Android Studio 2022-01-01
- LocationClient 与 LocationManager 2022-01-01
- 如何使 UINavigationBar 背景透明? 2022-01-01
- 如何使用 YouTube API V3? 2022-01-01