0

I've a problem with date picker. When I touch on textfield(datumletu) I call picker and datumletu [resign firstresponder]. But no effect. Here is my code. I'm trying everything but nothing is working.

-(void)textFieldDidBeginEditing:(UITextField *)textField {    
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

if (textField == self.datumLetu) {
    [self.view endEditing:YES];
  [self.autoTextFieldPriletu resignFirstResponder];
  [self.autoTextField resignFirstResponder];
  [self.cisloLetu resignFirstResponder];

    [self.datumLetu resignFirstResponder];
    self.picker.hidden = YES;
    [self showPicker];

    CGRect rect = self.view.frame;

    if(rect.origin.y == 0)
    {
        rect.origin.y -= 110;
        rect.size.height += 110;
        [UIView animateWithDuration:0.2 animations:^{
            self.view.frame = rect;
        }];
        self.picker.frame = CGRectMake(0, screenHeight / 2 +  screenHeight / 3.2, screenWidth, screenHeight/2);

    }

and picker

-(void)showPicker
{
    [self showAction];
}
 -(void) showAction
 {
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
    self.picker = [[Picker alloc] initWithFrame:CGRectMake(0, screenHeight / 2 +  screenHeight / 10.5, screenWidth, screenHeight/2)];
[self.picker addTargetForDoneButton:self action:@selector(cancelPressed)];
[self.picker addTargetForCancelButton:self action:@selector(cancelPressed)];

[self.picker setMode:UIDatePickerModeDateAndTime];
[self.picker.picker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:self.picker];
self.picker.hidden = NO;

}

enter image description here

Any help is highly appreciated.

iPeter
  • 1,330
  • 10
  • 26
ankmara
  • 265
  • 3
  • 13
  • I had a similar problem, abut not sure if it's of interest. Probably way too late for you in any event. The problem I had was: I presented the picker, and during the process of presenting the picker, I tried to resign the first responder. That didn't work. I found I had to resign the first responder *before* starting the process of presenting the picker. – Chris Prince Aug 22 '17 at 23:55

1 Answers1

0

In your ViewController add the delegate of the UITextField :

@interface ViewController : UIViewController <UITextFieldDelegate>

While Creating the UITextField or in ViewDidLoad:

self.myTextField.delegate = self;

In your implementation file:

 #pragma mark - UITextFieldDelegate

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField resignFirstResponder];

   // show your picker here 

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return NO; 
}
Alaeddine
  • 6,104
  • 3
  • 28
  • 45