Replacing UITextField input with a UIDatePicker

百般思念 提交于 2019-12-05 06:00:49
Dilip Rajkumar

Try this code.. here I am putting an datepicker to a uitextfield.. it will have a done button at the top right navigation bar.. so by clicking done I will user can dismiss the datepicker.. the another best method is by putting a toolbar above the datepicker having the done button.. Try this it will work.. when changing the datepicker you can populate the text field.. Hope this helps..

see this stackoverflow link https://stackoverflow.com/a/4824319/763747 this will have the datepicker with done button as toolbar above the keybord..

#pragma mark -
#pragma mark - TextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{
    [textField resignFirstResponder];
    return TRUE;
}


- (void) textFieldDidBeginEditing:(UITextField *)textField {
    itsRightBarButton.title  = @"Done";
    itsRightBarButton.style = UIBarButtonItemStyleDone;
    itsRightBarButton.target = self;
    itsRightBarButton.action = @selector(doneAction:);
    if ([textField isEqual:itsIncidentDateTextField])
    {
        itsDatePicker = [[[UIDatePicker alloc] init] autorelease];
        itsDatePicker.datePickerMode = UIDatePickerModeDate;
        [itsDatePicker addTarget:self action:@selector(incidentDateValueChanged:) forControlEvents:UIControlEventValueChanged];
        //datePicker.tag = indexPath.row;
        textField.inputView = itsDatePicker;
    }
}

- (IBAction) incidentDateValueChanged:(id)sender{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMM d, yyyy"];
    itsIncidentDateTextField.text = [dateFormatter stringFromDate:[itsDatePicker date]];
    [dateFormatter release];
}
iOSMaster

I used a different method for disappearing the UIDatePicker.

create a XIB (nib) file and add to it a UIDatePicker then resize the view so it fits only the UIDatePicker, create a property (make it strong and nonatomic) in your ViewController (or whatever class your using, and synthesize of course).

@property (nonatomic, strong) UIView *myDatePickerView;  
@synthesize myDatePickerView;

then create a loadDatePickerView method

- (void) loadDatePickerView
{
    UINib *nib = [UINib nibWithNibName:kNIBname bundle:[NSBundle mainBundle]];
    NSArray *views = [nib instantiateWithOwner:self options:nil];
    self.myDatePickerView = [views firstObject];
    [myDatePickerView setFrame:CGRectMake(0, 318, 320, 162)];
    [self.view addSubview:myDatePickerView];
}

implement the UITextFieldDelegate, and in the textFieldDidBeginEditing method call the loadDatePickerView method,

[self loadDatePickerView];

to make function create a property which is a UIDatePicker instance

@property (nonatomic,strong) 

UIDatePicker *myDatePicker;

(synthesize of course)

now create an IBAction like so:

-(IBAction)datePickerValueChanged:(UIDatePicker *)sender
{
    myDatePicker = [[myDatePickerView subviews] lastObject];
    //now you can do whatever you want with the DatePicker
}

now connect the IBAction to the picker in the XIB file, that way the XIB is now the UIDatePicker instance you created in the VC, if you want it to disappear you can add a UITapGestureRecognizer (in the ViewDidLoad) and the selector will be another IBAction which removes myDatePickerView from its' superView like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self     action:@selector(dropPicker:)];
    [self.view addGestureRecognizer:tap];
    [self datePickerValueChanged:myDatePicker];
}

-(IBAction)dropPicker:(id)sender
{
    [myDatePickerView removeFromSuperview];
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!