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

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.

推荐答案

您可以为此使用文本字段的 inputViewinputAccessoryView 属性.创建日期选择器并将其设置为两个文本字段的输入视图.还为 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 并覆盖 inputViewinputAccessoryView.当有大量它们时,这是要走的路.

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.

这篇关于在点击文本字段时显示日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
HTTPS request using volley(使用 volley 的 HTTPS 请求)
Error: Name not maching for self signed SSL certificates on Android(错误:Android 上自签名 SSL 证书的名称不匹配)
Android 7.0 : #39;javax.net.ssl.SSLHandshakeException: Connection closed by peer(Android 7.0:javax.net.ssl.SSLHandshakeException:连接被对等方关闭)
Is it possible to generate certificate signing request(.csr) using security framework in iOS?(是否可以在 iOS 中使用安全框架生成证书签名请求(.csr)?)