iPhone datepicker instead of keyboard?

后端 未结 5 725
星月不相逢
星月不相逢 2020-12-07 17:43

How would you do that?

I have a form with a UITextField. When I click in it I would like to display a UIDatePicker instead of the default Keyboard that pops-up by de

相关标签:
5条回答
  • 2020-12-07 18:21

    I think this is a more official way to do this:

    UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    datePicker.tag = indexPath.row;
    textField.inputView = datePicker;
    
    0 讨论(0)
  • 2020-12-07 18:27

    I've converted Johan Kool's answer into swift if anyone would like it. I placed the code in viewDidLoad.

    let datePicker = UIDatePicker()
    datePicker.datePickerMode = UIDatePickerMode.Date
    datePicker.addTarget(self, action: "datePickerValueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    textField.inputView = datePicker
    
    0 讨论(0)
  • 2020-12-07 18:33

    Added in .h files :

    UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> 
    

    Added in .m files :

    {
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    
    df.dateStyle = NSDateFormatterMediumStyle;
    
    pTxtDate.text = [NSString stringWithFormat:@"%@",
    
    [df stringFromDate:[NSDate date]]];
    
    NSLog(@"pTxtDate.text %@",pTxtDate.text);
    
    [df release];
    
    [self.view addSubview:pTxtDate]; 
    
    [pTxtDate release];
    
    DatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
    
    DatePicker.datePickerMode = UIDatePickerModeDate;
    
    DatePicker.hidden = NO;
    
    DatePicker.date = [NSDate date];
    
    [DatePicker addTarget:self
    
      action:@selector(LabelChange:)
    
    forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:DatePicker];
    
        [DatePicker release];
    
    }
    
    
    
    - (void)LabelChange:(id)sender {
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    
    df.dateStyle = NSDateFormatterMediumStyle;
    
    pTxtDate.text = [NSString stringWithFormat:@"%@",
    
    [df stringFromDate:DatePicker.date]];
    
    }
    
    0 讨论(0)
  • 2020-12-07 18:34

    in .h

    UIDatePicker *datePicker;
    NSDate *date;
    UIToolbar *keyboardToolbar;
    

    in ViewDidLoad:

    if (keyboardToolbar == nil) {
        keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
        [keyboardToolbar setBarStyle:UIBarStyleBlackTranslucent];
    
        UIBarButtonItem *extraSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
        UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:@"Accept" style:UIBarButtonItemStyleDone target:self action:@selector(closeKeyboard:)];
    
        [keyboardToolbar setItems:[[NSArray alloc] initWithObjects: extraSpace, accept, nil]];
    }
    
    self.txtDate.inputAccessoryView = keyboardToolbar;
    
    datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    self.txtDate.inputView = datePicker;
    
    
    - (void)datePickerValueChanged:(id)sender{
    
    date = datePicker.date;
    
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"dd/MM/YYYY"];
    
    
    [self.txtDate setText:[df stringFromDate:date]];
    }
    

    in storyboard add to txtDate -> EditingDidEnd

    - (IBAction)establishHour:(id)sender{
    
    hour = hourPicker.date;
    
    NSDateFormatter *hf = [[NSDateFormatter alloc] init];
    [hf setDateFormat:@"HH:MM"];
    
    [self.txtHour setText:[hf stringFromDate:hour]];
    
    }
    

    Sets the keyboard as DatePicker and created a toolbar with a button to accept the selection.

    0 讨论(0)
  • 2020-12-07 18:39
    (IBAction)estableceHora:(id)sender{
       Hora = horaPicker.date;
       NSDateFormatter *hf = [[NSDateFormatter alloc] init];
       [hf setDateFormat:@"HH:MM"];
       [self.txtHora setText:[hf stringFromDate:Hora]];
    }
    
    0 讨论(0)
提交回复
热议问题