Replacing UITextField input with a UIDatePicker

2019-04-28 15:52发布

I have a table view controller with (among others) a cell that is to represent a date. I followed this post "How do I make a modal date picker that only covers half the screen?" and I have it working with one big exception - I can't get the picker to disappear!

I tried registering for the event UIControlEventTouchUpOutside, but it seems that this is not generated by the picker (or at least in the mode that I am using it).

How can I recognize that the user has finished selecting a date?

Also, is there a way to disable the user from inputting directly into the UITextField? I want to force them to use the picker. I saw this post "Disable blinking cursor in UITextField?", but is there another way?

Reagards,

--John

2条回答
【Aperson】
2楼-- · 2019-04-28 16:24

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];
} 
查看更多
【Aperson】
3楼-- · 2019-04-28 16:30

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];
}
查看更多
登录 后发表回答