How to display UIDatePicker if user hit the UITextfield Programmatically

后端 未结 7 1321
刺人心
刺人心 2021-02-05 14:31

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

7条回答
  •  被撕碎了的回忆
    2021-02-05 15:13

    This will be simple, try this
    When U click the textField, this method is called.

    -(void)textFieldDidBeginEditing:(UITextField *)textField
    {  
    

    Then, we dont want to display the keyboard.

    [textField resignFirstResponder];  
    

    Then, to display the datePicker

    datePicker=[[UIDatePicker alloc]init];
    [datePicker setFrame:CGRectMake()];
    datePicker.datePickerMode=UIDatePickerModeDateAndTime;
    datePicker.timeZone = [NSTimeZone localTimeZone];
    [datePicker addTarget:self action:@selector(done) forControlEvents:UIControlEventValueChanged];  
    

    This will display the datePicker.
    Then U want to display the selected value in textField

    -(void)done
    {
      NSString *dateStri=[dateFormater stringFromDate:datePicker.date];
        [timingTextField setText:[NSString stringWithFormat:@"%@",dateStri]];
    }  
    

    Try this, and update me the result

提交回复
热议问题