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.
这篇关于在点击文本字段时显示日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在点击文本字段时显示日期选择器
基础教程推荐
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
