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

发布于 2024-11-09 10:11:59 字数 103 浏览 6 评论 0原文

如何在点击文本框时显示日期时间选择器控件?

我有一个具有到达和出发文本字段的用户界面,当用户单击到达文本框时,它应该调出一个日期时间选择器控件而不是键盘,并且与出发文本框相同。

How can I display a datetimepicker control on tap of a textbox?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清君侧 2024-11-16 10:11:59

您可以使用文本字段的 inputViewinputAccessoryView 属性来实现此目的。创建日期选择器并将其设置为两个文本字段的输入视图。还为 Done 按钮创建另一个视图,并将其作为其附属视图。您将需要该按钮来关闭输入视图。

Done 按钮必须连接到基本上执行此操作的函数 -

if ( [textField1 isFirstResponder] ) {
    [textField1 resignFirstResponder];
} else if ( [textField2 isFirstResponder] ) {
    [textField2 resignFirstResponder];
}

另一种选择是子类 UITextField 并覆盖 inputViewinputAccessoryView。当它们很多时,这是要走的路。

示例

@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:。把它们也连接起来。这些方法的实施如下所列。

@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

随着更多文本字段依赖于此选择器,最后两种方法将会变得臃肿。

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];
}

Another option would be to subclass UITextField and override inputView and inputAccessoryView. This is the way to go when there are loads of them.

Example

@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

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.

感性不性感 2024-11-16 10:11:59

很简单,在ViewController.h中将UITextFieldDelegate放在viewDidLoad后面。

txtFecha.delegate = self;

datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];

self.txtFecha.inputView = datePicker;

Very simple, in ViewController.h put the UITextFieldDelegate after in viewDidLoad.

txtFecha.delegate = self;

datePicker = [[UIDatePicker alloc]init];
[datePicker setDatePickerMode:UIDatePickerModeDate];

self.txtFecha.inputView = datePicker;
ゝ杯具 2024-11-16 10:11:59

在 viewDidLoad 中 ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    [datePicker setDate:[NSDate date]];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self action:@selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
    [txtFieldBranchYear setInputView:datePicker];
}

在 ViewController 中添加一个方法

-(void) dateTextField:(id)sender
{
    UIDatePicker *picker = (UIDatePicker*)txtFieldBranchYear.inputView;
    [picker setMaximumDate:[NSDate date]];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    NSDate *eventDate = picker.date;
    [dateFormat setDateFormat:@"dd/MM/yyyy"];

    NSString *dateString = [dateFormat stringFromDate:eventDate];
    txtFieldBranchYear.text = [NSString stringWithFormat:@"%@",dateString];
}

txtFieldBranchYear 是 UITextField 的一个出口< /em>

In viewDidLoad of ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    [datePicker setDate:[NSDate date]];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self action:@selector(dateTextField:) forControlEvents:UIControlEventValueChanged];
    [txtFieldBranchYear setInputView:datePicker];
}

Add one method in your ViewController

-(void) dateTextField:(id)sender
{
    UIDatePicker *picker = (UIDatePicker*)txtFieldBranchYear.inputView;
    [picker setMaximumDate:[NSDate date]];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    NSDate *eventDate = picker.date;
    [dateFormat setDateFormat:@"dd/MM/yyyy"];

    NSString *dateString = [dateFormat stringFromDate:eventDate];
    txtFieldBranchYear.text = [NSString stringWithFormat:@"%@",dateString];
}

txtFieldBranchYear is an outlet of UITextField

只怪假的太真实 2024-11-16 10:11:59

我发现了一个非常有用的课程 - 博客文章在这里:http://www.pietrorea.com/2012/07/how-to-hide-the-cursor-in-a-uitextfield/ 但它基本上使用inputView/inputAccessory 视图也是如此。更棒的是,UILabel,至少对于我正在做的事情来说,正是我所需要的。

There's a really useful class I found - the blog post is here: http://www.pietrorea.com/2012/07/how-to-hide-the-cursor-in-a-uitextfield/ but it basically uses the inputView/inputAccessory view as well. The kicker is that the UILabel, at least for what I was doing, was exactly what I needed.

等数载,海棠开 2024-11-16 10:11:59

单击文本字段打开日期选择器 swift 3.0
添加 UITextFieldDelegate 和
在 ViewDidload() 上设置委托
tfDate.delegate = 自我

//MARK:- TextField Delegate
//MARK:-

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

//MARK:- DatePicker
//MARK:-

func textFieldDidBeginEditing(_ textField: UITextField) {
     self.callDatePicker(textField: tfDate)
}
func callDatePicker(textField:UITextField){
    let datePickerView  : UIDatePicker = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.date
    textField.inputView = datePickerView
    datePickerView.addTarget(self, action: #selector(MyViewController.handleDatePicker), for: .valueChanged)
}

var str:String = ""
var str1:String = ""
var strStartDate:Date!
var strEndDate:Date!

func handleDatePicker(sender: UIDatePicker) {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy"
    print("Date :: \(dateFormatter.string(from: sender.date))")
    tfDate.text = dateFormatter.string(from: sender.date)


}

open date picker on click on textField swift 3.0
add UITextFieldDelegate and
set delegate on ViewDidload()
tfDate.delegate = self

//MARK:- TextField Delegate
//MARK:-

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

//MARK:- DatePicker
//MARK:-

func textFieldDidBeginEditing(_ textField: UITextField) {
     self.callDatePicker(textField: tfDate)
}
func callDatePicker(textField:UITextField){
    let datePickerView  : UIDatePicker = UIDatePicker()
    datePickerView.datePickerMode = UIDatePickerMode.date
    textField.inputView = datePickerView
    datePickerView.addTarget(self, action: #selector(MyViewController.handleDatePicker), for: .valueChanged)
}

var str:String = ""
var str1:String = ""
var strStartDate:Date!
var strEndDate:Date!

func handleDatePicker(sender: UIDatePicker) {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy"
    print("Date :: \(dateFormatter.string(from: sender.date))")
    tfDate.text = dateFormatter.string(from: sender.date)


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文