如果用户以编程方式击中UITextfield,如何显示UIDatePicker [英] How to display UIDatePicker if user hit the UITextfield Programmatically

查看:60
本文介绍了如果用户以编程方式击中UITextfield,如何显示UIDatePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在用户单击UITextField时显示UIDatePicker.选择日期后,它应该显示在同一UITextField中.我想以编程方式实现UIDatePicker.我知道如何以编程方式编写UITextField的代码. 我什至不知道如何调用UIDatePicker.

I'd like to Display UIDatePicker only when user the UITextField is Clicked. When the date is picked, it should be shown in the same UITextField.I want to implement UIDatePicker programmatically.I know how to write the code of UITextField Programmatically. I even don't know how to call the UIDatePicker.

出生日期的日期

UITextField txtDOB=[[UITextField alloc]initWithFrame:CGRectMake(10, 190, 300, 20)];

txtDOB.borderStyle=UITextBorderStyleNone;

txtDOB.backgroundColor=[UIColor clearColor];

[txtDOB setUserInteractionEnabled:NO];

txtDOB.font=[UIFont systemFontOfSize:14.0];

txtDOB.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;

txtDOB.textAlignment=NSTextAlignmentLeft;

txtDOB.delegate=self;

[self.view addSubview:txtDOB];

推荐答案

最简单的方法是实现datePicker,如下所示:

The easiest way is to implement the datePicker is as following:

创建datePicker:

self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
[self.datePicker addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

然后将datePicker链接到textfield inputView:

Then link the datePicker to the textfield inputView:

self.textField.inputView = self.pickerView;

最后,您可以在选择日期时捕获动作,并将日期转换为字符串,以在textField

In the end you can catch the action when a date was picked, and convert the date to a string, to show the selected date in the textField

- (void)onDatePickerValueChanged:(UIDatePicker *)datePicker
{
    self.textField.text = [self.dateFormatter stringFromDate:datePicker.date];
}

这篇关于如果用户以编程方式击中UITextfield,如何显示UIDatePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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