显示在轻拍在textfield的datepicker

时间:2011-05-20 16:07:31

标签: ios4 xcode4 uitextfield uidatepicker

如何在文本框中点击显示datetimepicker控件?

我有一个具有到达和离开文本字段的用户界面,当用户点击到达文本框时,它应该调出datetimepicker控件而不是键盘,并且与出发文本框相同。

5 个答案:

答案 0 :(得分:42)

您可以使用文本字段的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中,拉出UIToolbarUIDatePicker,但不要将其附加到视图中。适当连接插座。 dateChanged:响应日期选择器中的更改,并在单击工具栏中的doneEditing:按钮时调用Done。也可以连接它们。这些方法如下所示实现。

@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

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

答案 1 :(得分:11)

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

txtFecha.delegate = self;

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

self.txtFecha.inputView = datePicker;

答案 2 :(得分:3)

在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的出口

答案 3 :(得分:1)

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

答案 4 :(得分:0)

点击textField swift 3.0打开日期选择器 添加UITextFieldDelegate和 在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)


}