显示在轻拍在textfield的datepicker [英] Display datepicker on tapping on textfield

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

问题描述

如何在文本框中点击显示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 此文本字段的属性。创建日期选择器并将其设置为两个文本字段的输入视图。还为完成按钮创建另一个视图,并将其作为附件视图。您将需要该按钮来关闭输入视图。

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.

完成按钮必须连接到功能基本上这样做 -

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:响应日期选择器中的更改, doneEditing:在完成时调用单击工具栏中的/ code>按钮。也可以连接它们。这些方法如下所示实现。

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.

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

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