How to display UIDatePicker if user hit the UITextfield Programmatically

后端 未结 7 1319
刺人心
刺人心 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:05

    Before going to this code, you need to just know about UIActionSheet and You need to implement UIActionShetDelegate and UITextFieldDelegate In the textFieldDidBeginEditing you need to show the action sheet as below:

    -(void)textFieldDidBeginEditing:(UITextField *)sender
    {
       UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:@"Select Date" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
      [asheet showInView:[self.view superview]];
      [asheet setFrame:CGRectMake(0, 117, 320, 383)];
    }
    
    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
      {
        UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 240)];
    
       [pickerView setMinuteInterval:5];
       [pickerView setTag: DatePickerTag];
    
      [actionSheet addSubview:pickerView];
      NSArray *subviews = [actionSheet subviews];
    
      [[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 250, 280, 46)];
      [[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 300, 280, 46)];
    
    }
    

提交回复
热议问题